
******************************************;
** Final Program for Selecting Plans    **;
** Combined Steps for exclusion         **;
** Added HEDIS 2012 data on 1-22-2015   **;
******************************************;

options ls=120 nofmterr;

libname plan '/data/Medicare_P01_2009/data/HEDIS/plandata';
libname new  '/data/Medicare_P01_2009/data/HEDIS/sasdata';


 *** Read In All Plan Level Datasets ***;

  %macro comb1(indat,outdat,year,crdat);

      data a; set &indat;

           if plantypec='HMO';
           year=&year;

           if tot_n>=500;
           proc sort; by org_id;

     data c; set &crdat;

           proc sort; by org_id;

     data &outdat; merge a(in=ok) c; by org_id;

          if ok;
 
  %mend;

  %comb1(plan.cntr2003,a03,2003,new.cr2003); 
  %comb1(plan.cntr2004,a04,2004,new.cr2004);
  %comb1(plan.cntr2005,a05,2005,new.cr2005);
  %comb1(plan.cntr2006,a06,2006,new.cr2006);
  %comb1(plan.cntr2007,a07,2007,new.cr2007);
  %comb1(plan.cntr2008,a08,2008,new.cr2008);
  %comb1(plan.cntr2009,a09,2009,new.cr2009);
  %comb1(plan.cntr2010,a10,2010,new.cr2010);
  %comb1(plan.cntr2011,a11,2011,new.cr2011);
  %comb1(plan.cntr2012,a12,2012,new.cr2012); 

  data comb00; set a03 a04 a05 a06 a07 a08 a09 a10 a11 a12;
       
         if year in (2003,2004,2005) then cmc=.;

         proc means mean noprint data=comb00 vardef=weight nway;
              class year;
                var bcs       cmc       cdc1      cdc3     cdc4     gso
                    amb_opv   amb_erv   amb_asp   amb_ors
                    iptotdays ipmeddays ipsurdays iptotdis ipmeddis ipsurdis
                    n_cabg    n_ptca    n_cc      n_ce     n_rff    n_thr    n_tkr
                    n_xli     n_oc      n_clc     n_ah     n_vh     n_p      n_mast
                    n_lump    n_bs      n_bwls;
             weight tot_n;
             output out =gm0
                    mean= gm_bcs  gm_cmc  gm_cdc1 gm_cdc3 gm_cdc4  gm_gso
                          gm_opv  gm_erv  gm_asp  gm_ors
                          gm_tday gm_mday gm_sday gm_tdis gm_mdis  gm_sdis
                          gm_cabg gm_ptca gm_cc   gm_ce   gm_rff   gm_thr   gm_tkr
                          gm_xli  gm_oc   gm_clc  gm_ah   gm_vh    gm_p     gm_mast
                          gm_lump gm_bs   gm_bwls;

           data gm(drop=_FREQ_ _TYPE_); set gm0;
              
                proc sort; by year;

 data comb0; merge comb00 gm; by year;

       ***********************************************************;
       ** Creating Indicator For Rule 1 (zero Plans) exclusion  **;
       ***********************************************************;

         nc_bcs = totn_bcs*0.75*non0_bcs ;
         nc_cmc = totn_cmc*0.75*non0_cmc ;
         nc_cdc1= totn_cdc1*0.75*non0_cdc1;   
         nc_cdc3= totn_cdc3*0.75*non0_cdc3;
         nc_cdc4= totn_cdc4*0.75*non0_cdc4;
         nc_gso = tot_n*0.75*non0_gso ;

         nc_tdis= tot_n*0.75*non0_tdis;
         nc_sdis= tot_n*0.75*non0_sdis;
         nc_mdis= tot_n*0.75*non0_mdis;
         nc_tday= tot_n*0.75*non0_tday;
         nc_sday= tot_n*0.75*non0_sday;
         nc_mday= tot_n*0.75*non0_mday;
         nc_opv = tot_n*0.75*non0_opv;
         nc_erv = tot_n*0.75*non0_erv;
         nc_asp = tot_n*0.75*non0_asp;
         nc_ors = tot_n*0.75*non0_ors;

         nc_cabg= tot_n*0.75*non0_cabg ;
         nc_ptca= tot_n*0.75*non0_ptca ;
         nc_cc  = tot_n*0.75*non0_cc ;
         nc_ce  = tot_n*0.75*non0_ce ;
         nc_rff = tot_n*0.75*non0_rff ;
         nc_thr = tot_n*0.75*non0_thr ;
         nc_tkr = tot_n*0.75*non0_tkr ;
         nc_xli = tot_n*0.75*non0_xli ;
         nc_oc  = tot_n*0.75*non0_oc ;
         nc_clc = tot_n*0.75*non0_clc ;
         nc_ah  = totn_ah*0.75*non0_ah ;
         nc_vh  = totn_vh*0.75*non0_vh ;
         nc_p   = totn_p*0.75*non0_p ;
         nc_mast= totn_mast*0.75*non0_mast;
         nc_lump= totn_lump*0.75*non0_lump;
         nc_bs  = tot_n*0.76*non0_bs;
         nc_bwls= tot_n*0.76*non0_bwls;

       if bcs=0       and nc_bcs>3  then d1_bcs=1;
       if cmc=0       and nc_cmc>3  then d1_cmc=1;
       if cdc1=0      and nc_cdc1>3 then d1_cdc1=1;
       if cdc3=0      and nc_cdc3>3 then d1_cdc3=1;
       if cdc4=0      and nc_cdc4>3 then d1_cdc4=1;
       if gso=0       and nc_gso>3  then d1_gso=1;

       if amb_opv=0   and nc_opv>3  then d1_opv=1;
       if amb_erv=0   and nc_erv>3  then d1_erv=1;
       if amb_asp=0   and nc_asp>3  then d1_asp=1;
       if amb_ors=0   and nc_ors>3  then d1_ors=1;
       if iptotdays=0 and nc_tday>3 then d1_tday=1;
       if ipmeddays=0 and nc_mday>3 then d1_mday=1;
       if ipsurdays=0 and nc_sday>3 then d1_sday=1;
       if iptotdis=0  and nc_tdis>3 then d1_tdis=1;
       if ipmeddis=0  and nc_mdis>3 then d1_mdis=1;
       if ipsurdis=0  and nc_sdis>3 then d1_sdis=1;

       if n_cabg=0    and nc_cabg>3 then d1_cabg=1;
       if n_ptca=0    and nc_ptca>3 then d1_ptca=1;
       if n_cc=0      and nc_cc>3   then d1_cc=1;
       if n_ce=0      and nc_ce>3   then d1_ce=1;
       if n_rff=0     and nc_rff>3  then d1_rff=1;
       if n_thr=0     and nc_thr>3  then d1_thr=1;
       if n_tkr=0     and nc_tkr>3  then d1_tkr=1;
       if n_xli=0     and nc_xli>3  then d1_xli=1;
       if n_oc=0      and nc_oc>3   then d1_oc=1;
       if n_clc=0     and nc_clc>3  then d1_clc=1;
       if n_ah=0      and nc_ah>3   then d1_ah=1;
       if n_vh=0      and nc_vh>3   then d1_vh=1; 
       if n_p=0       and nc_p>3    then d1_p=1; 
       if n_mast=0    and nc_mast>3 then d1_mast=1;
       if n_lump=0    and nc_lump>3 then d1_lump=1;
       if n_bs=0      and nc_bs>3   then d1_bs=1;
       if n_bwls=0    and nc_bwls>3 then d1_bwls=1;

     **********************************************************************;
     ** Create Indicators for Rule 2 (plans < 1/3 grand mean) exclusions **;
     **********************************************************************;

         if hn_bcs>0  then nc2_bcs = 0.3333*non0_bcs  - 1.96*sqrt(0.3333*non0_bcs /hn_bcs);
         if hn_cmc>0  then nc2_cmc = 0.3333*non0_cmc  - 1.96*sqrt(0.3333*non0_cmc /hn_cmc);
         if hn_cdc1>0 then nc2_cdc1= 0.3333*non0_cdc1 - 1.96*sqrt(0.3333*non0_cdc1/hn_cdc1);
         if hn_cdc3>0 then nc2_cdc3= 0.3333*non0_cdc3 - 1.96*sqrt(0.3333*non0_cdc3/hn_cdc3);
         if hn_cdc4>0 then nc2_cdc4= 0.3333*non0_cdc4 - 1.96*sqrt(0.3333*non0_cdc4/hn_cdc4);
         if hn_gso>0  then nc2_gso = 0.3333*non0_gso  - 1.96*sqrt(0.3333*non0_gso /hn_gso);

         nc2_tdis= 0.3333*non0_tdis - 1.96*sqrt(0.3333*non0_tdis/hn_tdis);
         nc2_sdis= 0.3333*non0_sdis - 1.96*sqrt(0.3333*non0_sdis/hn_sdis);
         nc2_mdis= 0.3333*non0_mdis - 1.96*sqrt(0.3333*non0_mdis/hn_mdis);
         nc2_tday= 0.3333*non0_tday - 1.96*sqrt(0.3333*non0_tday/hn_tday);
         nc2_sday= 0.3333*non0_sday - 1.96*sqrt(0.3333*non0_sday/hn_sday);
         nc2_mday= 0.3333*non0_mday - 1.96*sqrt(0.3333*non0_mday/hn_mday);
         nc2_opv = 0.3333*non0_opv  - 1.96*sqrt(0.3333*non0_opv /hn_opv);
         nc2_erv = 0.3333*non0_erv  - 1.96*sqrt(0.3333*non0_erv /hn_erv);
         nc2_asp = 0.3333*non0_asp  - 1.96*sqrt(0.3333*non0_asp /hn_asp);
         nc2_ors = 0.3333*non0_ors  - 1.96*sqrt(0.3333*non0_ors /hn_ors);

         nc2_cabg= 0.3333*non0_cabg - 1.96*sqrt(0.3333*non0_cabg/hn_cabg);
         nc2_ptca= 0.3333*non0_ptca - 1.96*sqrt(0.3333*non0_ptca/hn_ptca);
         nc2_cc  = 0.3333*non0_cc   - 1.96*sqrt(0.3333*non0_cc  /hn_cc);
         nc2_ce  = 0.3333*non0_ce   - 1.96*sqrt(0.3333*non0_ce  /hn_ce);
         nc2_rff = 0.3333*non0_rff  - 1.96*sqrt(0.3333*non0_rff /hn_rff);
         nc2_thr = 0.3333*non0_thr  - 1.96*sqrt(0.3333*non0_thr /hn_thr);
         nc2_tkr = 0.3333*non0_tkr  - 1.96*sqrt(0.3333*non0_tkr /hn_tkr);
         nc2_xli = 0.3333*non0_xli  - 1.96*sqrt(0.3333*non0_xli /hn_xli);
         nc2_oc  = 0.3333*non0_oc   - 1.96*sqrt(0.3333*non0_oc  /hn_oc );
         nc2_clc = 0.3333*non0_clc  - 1.96*sqrt(0.3333*non0_clc /hn_clc);

         if hn_ah>0   then nc2_ah  = 0.3333*non0_ah   - 1.96*sqrt(0.3333*non0_ah  /hn_ah);
         if hn_vh>0   then nc2_vh  = 0.3333*non0_vh   - 1.96*sqrt(0.3333*non0_vh  /hn_vh);
         if hn_p>0    then nc2_p   = 0.3333*non0_p    - 1.96*sqrt(0.3333*non0_p   /hn_p);
         if hn_mast>0 then nc2_mast= 0.3333*non0_mast - 1.96*sqrt(0.3333*non0_mast/hn_mast);
         if hn_lump>0 then nc2_lump= 0.3333*non0_lump - 1.96*sqrt(0.3333*non0_lump/hn_lump);
         if hn_bs>0   then nc2_bs  = 0.3333*non0_bs   - 1.96*sqrt(0.3333*non0_bs  /hn_bs);
         if hn_bwls>0 then nc2_bwls= 0.3333*non0_bwls - 1.96*sqrt(0.3333*non0_bwls/hn_bwls);
  
       if (bcs >0  and bcs  < gm_bcs/3)  and (hnon0_bcs < nc2_bcs)  then d2_bcs=1; 
       if (cmc >0  and cmc  < gm_cmc/3)  and (hnon0_cmc < nc2_cmc)  then d2_cmc=1;
       if (cdc1 >0 and cdc1 < gm_cdc1/3) and (hnon0_cdc1< nc2_cdc1) then d2_cdc1=1;
       if (cdc3 >0 and cdc3 < gm_cdc3/3) and (hnon0_cdc3< nc2_cdc3) then d2_cdc3=1;
       if (cdc4 >0 and cdc4 < gm_cdc4/3) and (hnon0_cdc4< nc2_cdc4) then d2_cdc4=1;
       if (gso >0  and gso  < gm_gso/3)  and (hnon0_gso < nc2_gso)  then d2_gso=1;

       if (amb_opv >0 and amb_opv < gm_opv/3) and (hnon0_opv < nc2_opv) then d2_opv=1;
       if (amb_erv >0 and amb_erv < gm_erv/3) and (hnon0_erv < nc2_erv) then d2_erv=1;
       if (amb_asp >0 and amb_asp < gm_asp/3) and (hnon0_asp < nc2_asp) then d2_asp=1;
       if (amb_ors >0 and amb_ors < gm_ors/3) and (hnon0_ors < nc2_ors) then d2_ors=1;

       if (iptotdays>0 and iptotdays < gm_tday/3) and (hnon0_tday < nc2_tday) then d2_tday=1;
       if (ipmeddays>0 and ipmeddays < gm_mday/3) and (hnon0_mday < nc2_mday) then d2_mday=1;
       if (ipsurdays>0 and ipsurdays < gm_sday/3) and (hnon0_sday < nc2_sday) then d2_sday=1;
       if (iptotdis>0  and iptotdis < gm_tdis/3)  and (hnon0_tdis < nc2_tdis) then d2_tdis=1;
       if (ipmeddis>0  and ipmeddis < gm_mdis/3)  and (hnon0_mdis < nc2_mdis) then d2_mdis=1;
       if (ipsurdis>0  and ipsurdis < gm_sdis/3)  and (hnon0_sdis < nc2_sdis) then d2_sdis=1;

       if (n_cabg>0 and n_cabg < gm_cabg/3) and (hnon0_cabg < nc2_cabg) then d2_cabg=1;
       if (n_ptca>0 and n_ptca < gm_ptca/3) and (hnon0_ptca < nc2_ptca) then d2_ptca=1;
       if (n_cc>0   and n_cc   < gm_cc/3)   and (hnon0_cc   < nc2_cc)   then d2_cc=1;
       if (n_ce>0   and n_ce   < gm_ce/3)   and (hnon0_ce   < nc2_ce)   then d2_ce=1;
       if (n_rff>0  and n_rff  < gm_rff/3)  and (hnon0_rff  < nc2_rff)  then d2_rff=1;
       if (n_thr>0  and n_thr  < gm_thr/3)  and (hnon0_thr  < nc2_thr)  then d2_thr=1;
       if (n_tkr>0  and n_tkr  < gm_tkr/3)  and (hnon0_tkr  < nc2_tkr)  then d2_tkr=1;
       if (n_xli>0  and n_xli  < gm_xli/3)  and (hnon0_xli  < nc2_xli)  then d2_xli=1;
       if (n_oc>0   and n_oc   < gm_oc/3)   and (hnon0_oc   < nc2_oc)   then d2_oc=1;
       if (n_clc>0  and n_clc  < gm_clc/3)  and (hnon0_clc  < nc2_clc)  then d2_clc=1;
       if (n_ah>0   and n_ah   < gm_ah/3)   and (hnon0_ah   < nc2_ah)   then d2_ah=1;
       if (n_vh>0   and n_vh   < gm_vh/3)   and (hnon0_vh   < nc2_vh)   then d2_vh=1; 
       if (n_p>0    and n_p    < gm_p/3)    and (hnon0_p    < nc2_p)    then d2_p=1; 
       if (n_mast>0 and n_mast < gm_mast/3) and (hnon0_mast < nc2_mast) then d2_mast=1;
       if (n_lump>0 and n_lump < gm_lump/3) and (hnon0_lump < nc2_lump) then d2_lump=1;
       if (n_bs>0   and n_bs   < gm_bs/3)   and (hnon0_bs   < nc2_bs)   then d2_bs=1;
       if (n_bwls>0 and n_bwls < gm_bwls/3) and (hnon0_bwls < nc2_bwls) then d2_bwls=1;

     ********************************************************;
     ** Creat Indicators for Rule 3 exclusions -iterating  **;
     ********************************************************;
    
      *-- Step 1. Create a set of variables after rule 1 & 2 exclusions --*;
      
       bcs_3  = bcs;  if d1_bcs=1  or d2_bcs=1  then bcs_3=.;
       cmc_3  = cmc;  if d1_cmc=1  or d2_cmc=1  then cmc_3=.;
       cdc1_3 = cdc1; if d1_cdc1=1 or d2_cdc1=1 then cdc1_3=.; 
       cdc3_3 = cdc3; if d1_cdc3=1 or d2_cdc3=1 then cdc3_3=.;
       cdc4_3 = cdc4; if d1_cdc4=1 or d2_cdc4=1 then cdc4_3=.;
       gso_3  = cdc1; if d1_gso=1  or d2_gso=1  then gso_3=.;

       opv = amb_opv; if d1_opv=1 or d2_opv=1 then opv=.;
       erv = amb_erv; if d1_erv=1 or d2_erv=1 then erv=.;
       asp = amb_asp; if d1_asp=1 or d2_asp=1 then asp=.;
       ors = amb_ors; if d1_ors=1 or d2_ors=1 then ors=.;

       tday = iptotdays; if d1_tday=1 or d2_tday=1 then tday=.;
       mday = ipmeddays; if d1_mday=1 or d2_mday=1 then mday=.;
       sday = ipsurdays; if d1_sday=1 or d2_sday=1 then sday=.;
       tdis = iptotdis;  if d1_tdis=1 or d2_tdis=1 then tdis=.;
       mdis = ipmeddis;  if d1_mdis=1 or d2_mdis=1 then mdis=.;
       sdis = ipsurdis;  if d1_sdis=1 or d2_sdis=1 then sdis=.;

       cabg = n_cabg;  if d1_cabg=1 or d2_cabg=1 then cabg=.;
       ptca = n_ptca;  if d1_ptca=1 or d2_ptca=1 then ptca=.;
       cc   = n_cc  ;  if d1_cc  =1 or d2_cc=1   then cc=.; 
       ce   = n_ce  ;  if d1_ce  =1 or d2_ce=1   then ce=.;
       rff  = n_rff ;  if d1_rff =1 or d2_rff=1  then rff=.;
       thr  = n_thr ;  if d1_thr =1 or d2_thr=1  then thr=.;
       tkr  = n_tkr ;  if d1_tkr =1 or d2_tkr=1  then tkr=.;
       xli  = n_xli ;  if d1_xli =1 or d2_xli=1  then xli=.;
       oc   = n_oc  ;  if d1_oc  =1 or d2_oc =1  then oc=.;
       clc  = n_clc ;  if d1_clc =1 or d2_clc=1  then clc=.;
       ah   = n_ah  ;  if d1_ah  =1 or d2_ah=1   then ah=.;
       vh   = n_vh  ;  if d1_vh  =1 or d2_vh=1   then vh=.;
       p    = n_p   ;  if d1_p   =1 or d2_p=1    then p=.;
       mast = n_mast;  if d1_mast=1 or d2_mast=1 then mast=.;
       lump = n_lump;  if d1_lump=1 or d2_lump=1 then lump=.;
       bs   = n_bs;    if d1_bs=1   or d2_bs=1   then bs=.;
       bwls = n_bwls;  if d1_bwls=1 or d2_bwls=1 then bwls=.;

       if gm_bcs>0  then bcsr = bcs_3/gm_bcs;
       if gm_cmc>0  then cmcr = cmc_3/gm_cmc;
       if gm_cdc1>0 then cdc1r= cdc1_3/gm_cdc1;
       if gm_cdc3>0 then cdc3r= cdc3_3/gm_cdc3;
       if gm_cdc4>0 then cdc4r= cdc4_3/gm_cdc4;
       if gm_gso>0  then gsor = gso_3/gm_gso;

       if gm_opv>0 then opvr = opv/gm_opv;
       if gm_erv>0 then ervr = erv/gm_erv;
       if gm_asp>0 then aspr = asp/gm_asp;
       if gm_ors>0 then orsr = ors/gm_ors;

       if gm_tday>0 then tdayr = tday/gm_tday;
       if gm_mday>0 then mdayr = mday/gm_mday;
       if gm_sday>0 then sdayr = sday/gm_sday;
       if gm_tdis>0 then tdisr = tdis/gm_tdis;
       if gm_mdis>0 then mdisr = mdis/gm_mdis;
       if gm_sdis>0 then sdisr = sdis/gm_sdis;

       if gm_cabg>0 then cabgr = cabg/gm_cabg;
       if gm_ptca>0 then ptcar = ptca/gm_ptca;
       if gm_cc>0   then ccr   = cc/gm_cc;
       if gm_ce>0   then cer   = ce/gm_ce;
       if gm_rff>0  then rffr  = rff/gm_rff;
       if gm_thr>0  then thrr  = thr/gm_thr;
       if gm_tkr>0  then tkrr  = tkr/gm_tkr;
       if gm_xli>0  then xlir  = xli/gm_xli;
       if gm_oc>0   then ocr   = oc /gm_oc ;
       if gm_clc>0  then clcr  = clc/gm_clc;
       if gm_ah>0   then ahr   = ah/gm_ah ;
       if gm_vh>0   then vhr   = vh/gm_vh ;
       if gm_p>0    then pr    = p/gm_p ;
       if gm_mast>0 then mastr = mast/gm_mast;
       if gm_lump>0 then lumpr = lump/gm_lump;
       if gm_bs>0   then bsr   = bs/gm_bs;
       if gm_bwls>0 then bwlsr = bwls/gm_bwls;
       
      *-- Step 2 Cross year of Mean and SD of for each plan --*;

      proc means data=comb0 noprint mean std nway;
           class org_id;
           var   bcs_3 cmc_3 cdc1_3 cdc3_3 cdc4_3 gso_3
                 opv   erv   asp  ors  tday  mday  sday  tdis  mdis  sdis
                 cabg  ptca  cc   ce   rff   thr   tkr   xli   oc    clc      
                 ah    vh    p    mast lump  bs    bwls;
          output out = ms0 
                 mean= mean_bcs  mean_cmc  mean_cdc1 mean_cdc3 mean_cdc4 mean_gso
                       mean_opv  mean_erv  mean_asp  mean_ors  mean_tday mean_mday mean_sday mean_tdis mean_mdis mean_sdis
                       mean_cabg mean_ptca mean_cc   mean_ce   mean_rff  mean_thr  mean_tkr  mean_xli  mean_oc   mean_clc
                       mean_ah   mean_vh   mean_p    mean_mast mean_lump mean_bs   mean_bwls 
                 std = std_bcs  std_cmc  std_cdc1 std_cdc3 std_cdc4 std_gso
                       std_opv  std_erv  std_asp  std_ors  std_tday std_mday std_sday std_tdis std_mdis std_sdis
                       std_cabg std_ptca std_cc   std_ce   std_rff  std_thr  std_tkr  std_xli  std_oc   std_clc
                       std_ah   std_vh   std_p    std_mast std_lump std_bs   std_bwls;
  
      *-- Step 3 Obtain Mean of STDs --*;

     data ms(drop=_FREQ_ _TYPE_); set ms0;

          cons=1;
          proc sort; by cons;

          proc means data=ms mean noprint nway;
                 var   std_bcs  std_cmc  std_cdc1 std_cdc3 std_cdc4 std_gso
                       std_opv  std_erv  std_asp  std_ors  std_tday std_mday std_sday std_tdis std_mdis std_sdis
                       std_cabg std_ptca std_cc   std_ce   std_rff  std_thr  std_tkr  std_xli  std_oc   std_clc
                       std_ah   std_vh   std_p    std_mast std_lump std_bs   std_bwls ; 
             output out = s00 
                   mean = gstd_bcs  gstd_cmc  gstd_cdc1 gstd_cdc3 gstd_cdc4 gstd_gso
                          gstd_opv  gstd_erv  gstd_asp  gstd_ors  gstd_tday gstd_mday gstd_sday gstd_tdis gstd_mdis gstd_sdis
                          gstd_cabg gstd_ptca gstd_cc   gstd_ce   gstd_rff  gstd_thr  gstd_tkr  gstd_xli  gstd_oc   gstd_clc
                          gstd_ah   gstd_vh   gstd_p    gstd_mast gstd_lump gstd_bs   gstd_bwls ; 
     
      data s0(drop=_FREQ_ _TYPE_); set s00;  
           cons=1;
           proc sort; by cons;


      *-- Step 4 Combine Cross Year Mean and STD, and Mean of STD --*;

      data b0(drop=cons); merge ms s0; by cons;

           proc sort; by org_id;

  
   *--------------------------------------------------*; 
   *  Repeat Steps 2 to 4 for Standardized Variables  *;
   *--------------------------------------------------*;

     ** Note:  No Need to add N_BWLS for rule 3 since 2010 is the first year of data **;


      proc means data=comb0 noprint mean std nway;
           class org_id;
           var   bcsr cmcr cdc1r cdc3r cdc4r gsor
                 opvr   ervr   aspr  orsr  tdayr  mdayr  sdayr  tdisr  mdisr  sdisr
                 cabgr  ptcar  ccr   cer   rffr   thrr   tkrr   xlir   ocr    clcr
                 ahr    vhr    pr    mastr lumpr  bsr    bwlsr;
          output out = ms0r
                 mean= mean_bcsr  mean_cmcr  mean_cdc1r mean_cdc3r mean_cdc4r mean_gsor
                       mean_opvr  mean_ervr  mean_aspr  mean_orsr  mean_tdayr mean_mdayr mean_sdayr mean_tdisr mean_mdisr mean_sdisr
                       mean_cabgr mean_ptcar mean_ccr   mean_cer   mean_rffr  mean_thrr  mean_tkrr  mean_xlir  mean_ocr   mean_clcr
                       mean_ahr   mean_vhr   mean_pr    mean_mastr mean_lumpr mean_bsr   mean_bwlsr
                 std = std_bcsr  std_cmcr  std_cdc1r std_cdc3r std_cdc4r std_gsor
                       std_opvr  std_ervr  std_aspr  std_orsr  std_tdayr std_mdayr std_sdayr std_tdisr std_mdisr std_sdisr
                       std_cabgr std_ptcar std_ccr   std_cer   std_rffr  std_thrr  std_tkrr  std_xlir  std_ocr   std_clcr
                       std_ahr   std_vhr   std_pr    std_mastr std_lumpr std_bsr   std_bwlsr;

     data msr(drop=_FREQ_ _TYPE_); set ms0r;

          cons=1;
          proc sort; by cons;

          proc means data=msr mean noprint nway;
                 var   std_bcsr  std_cmcr  std_cdc1r std_cdc3r std_cdc4r std_gsor
                       std_opvr  std_ervr  std_aspr  std_orsr  std_tdayr std_mdayr std_sdayr std_tdisr std_mdisr std_sdisr
                       std_cabgr std_ptcar std_ccr   std_cer   std_rffr  std_thrr  std_tkrr  std_xlir  std_ocr   std_clcr
                       std_ahr   std_vhr   std_pr    std_mastr std_lumpr std_bsr   std_bwlsr;
             output out = s00r
                   mean = gstd_bcsr  gstd_cmcr  gstd_cdc1r gstd_cdc3r gstd_cdc4r gstd_gsor
                          gstd_opvr  gstd_ervr  gstd_aspr  gstd_orsr  gstd_tdayr gstd_mdayr gstd_sdayr gstd_tdisr gstd_mdisr gstd_sdisr
                          gstd_cabgr gstd_ptcar gstd_ccr   gstd_cer   gstd_rffr  gstd_thrr  gstd_tkrr  gstd_xlir  gstd_ocr   gstd_clcr
                          gstd_ahr   gstd_vhr   gstd_pr    gstd_mastr gstd_lumpr gstd_bsr   gstd_bwlsr ;

      data s0r(drop=_FREQ_ _TYPE_); set s00r;
           cons=1;
           proc sort; by cons;

      data b0r(drop=cons); merge msr s0r; by cons;

           proc sort; by org_id;


 
  *************************************************;
  ** Transpose Data To 1 Plan Per Oberservation  **;
  *************************************************;

  %macro comb2(indat,outdat,year,yr);

       data c01; set &indat;

            if year=&year;
            totn_&yr = tot_n;

           *-- Original Plan Level Values --*;

            bcs_&yr = bcs;
            cmc_&yr = cmc;
            cdc1_&yr= cdc1;
            cdc3_&yr= cdc3;
            cdc4_&yr= cdc4;
            gso_&yr = gso;

            opv_&yr  = amb_opv;
            erv_&yr  = amb_erv;
            asp_&yr  = amb_asp;
            ors_&yr  = amb_ors;

            tday_&yr = iptotdays;
            mday_&yr = ipmeddays;
            sday_&yr = ipsurdays;
            tdis_&yr = iptotdis;
            mdis_&yr = ipmeddis;
            sdis_&yr = ipsurdis;

            cabg_&yr= n_cabg ;
            ptca_&yr= n_ptca;
            cc_&yr  = n_cc;
            ce_&yr  = n_ce;
            rff_&yr = n_rff;
            thr_&yr = n_thr;
            tkr_&yr = n_tkr;
            xli_&yr = n_xli;
            oc_&yr  = n_oc;
            clc_&yr = n_clc;
            ah_&yr  = n_ah;
            vh_&yr  = n_vh;
            p_&yr   = n_p;
            mast_&yr= n_mast;
            lump_&yr= n_lump;
            bs_&yr  = n_bs;
            bwls_&yr= n_bwls;

          *-- Standardized year specific values after rule 1&2 exclusion --*;

            bcsr_&yr = bcsr;
            cmcr_&yr = cmcr;
            cdc1r_&yr= cdc1r;
            cdc3r_&yr= cdc3r;
            cdc4r_&yr= cdc4r;
            gsor_&yr = gsor;

            opvr_&yr  = opvr;
            ervr_&yr  = ervr;
            aspr_&yr  = aspr;
            orsr_&yr  = orsr;

            tdayr_&yr = tdayr;
            mdayr_&yr = mdayr;
            sdayr_&yr = sdayr;
            tdisr_&yr = tdisr;
            mdisr_&yr = mdisr;
            sdisr_&yr = sdisr;

            cabgr_&yr= cabgr ;
            ptcar_&yr= ptcar;
            ccr_&yr  = ccr;
            cer_&yr  = cer;
            rffr_&yr = rffr;
            thrr_&yr = thrr;
            tkrr_&yr = tkrr;
            xlir_&yr = xlir;
            ocr_&yr  = ocr;
            clcr_&yr = clcr;
            ahr_&yr  = ahr;
            vhr_&yr  = vhr;
            pr_&yr   = pr;
            mastr_&yr= mastr;
            lumpr_&yr= lumpr;
            bsr_&yr  = bsr;
            bwlsr_&yr= bwlsr;

         *-- Grand Means for Each Year Each Measure --*;

            gmbcs_&yr = gm_bcs;
            gmcmc_&yr = gm_cmc;
            gmcdc1_&yr= gm_cdc1;
            gmcdc3_&yr= gm_cdc3;
            gmcdc4_&yr= gm_cdc4;
            gmgso_&yr = gm_gso;
         
            gmopv_&yr = gm_opv;
            gmerv_&yr = gm_erv;
            gmasp_&yr = gm_asp;  
            gmors_&yr = gm_ors;

            gmtday_&yr = gm_tday;  
            gmmday_&yr = gm_mday;  
            gmsday_&yr = gm_sday;  
            gmtdis_&yr = gm_tdis;  
            gmmdis_&yr = gm_mdis;  
            gmsdis_&yr = gm_sdis;

            gmcabg_&yr = gm_cabg;
            gmptca_&yr = gm_ptca; 
            gmcc_&yr   = gm_cc;   
            gmce_&yr   = gm_ce;   
            gmrff_&yr  = gm_rff;   
            gmthr_&yr  = gm_thr;   
            gmtkr_&yr  = gm_tkr;   
            gmxli_&yr  = gm_xli;   
            gmoc_&yr   = gm_oc;
            gmclc_&yr  = gm_clc;  
            gmah_&yr   = gm_ah;   
            gmvh_&yr   = gm_vh;   
            gmp_&yr    = gm_p;    
            gmmast_&yr = gm_mast;
            gmlump_&yr = gm_lump;
            gmbs_&yr   = gm_bs;            
            gmbwls_&yr = gm_bwls;

         *-- Rule 1 Exclusion Indicators--*;

            d1bcs_&yr = d1_bcs;
            d1cmc_&yr = d1_cmc;
            d1cdc1_&yr= d1_cdc1;
            d1cdc3_&yr= d1_cdc3;
            d1cdc4_&yr= d1_cdc4;
            d1gso_&yr = d1_gso;

            d1opv_&yr = d1_opv;
            d1erv_&yr = d1_erv;
            d1asp_&yr = d1_asp;
            d1ors_&yr = d1_ors;
            d1tday_&yr= d1_tday;
            d1mday_&yr= d1_mday;
            d1sday_&yr= d1_sday;
            d1tdis_&yr= d1_tdis;
            d1mdis_&yr= d1_mdis;
            d1sdis_&yr= d1_sdis;
  
            d1cabg_&yr= d1_cabg ;
            d1ptca_&yr= d1_ptca;
            d1cc_&yr  = d1_cc;
            d1ce_&yr  = d1_ce;
            d1rff_&yr = d1_rff;
            d1thr_&yr = d1_thr;
            d1tkr_&yr = d1_tkr;
            d1xli_&yr = d1_xli;
            d1oc_&yr  = d1_oc;
            d1clc_&yr = d1_clc;
            d1ah_&yr  = d1_ah;
            d1vh_&yr  = d1_vh;
            d1p_&yr   = d1_p;
            d1mast_&yr= d1_mast;
            d1lump_&yr= d1_lump;
            d1bs_&yr  = d1_bs;
            d1bwls_&yr= d1_bwls;

         *-- Rule 2 Indicators per measure --*;
         
            d2bcs_&yr = d2_bcs;
            d2cmc_&yr = d2_cmc;
            d2cdc1_&yr= d2_cdc1;
            d2cdc3_&yr= d2_cdc3;
            d2cdc4_&yr= d2_cdc4;

            d2opv_&yr = d2_opv;
            d2erv_&yr = d2_erv;
            d2asp_&yr = d2_asp;
            d2ors_&yr = d2_ors;
            d2tday_&yr= d2_tday;
            d2mday_&yr= d2_mday;
            d2sday_&yr= d2_sday;
            d2tdis_&yr= d2_tdis;
            d2mdis_&yr= d2_mdis;
            d2sdis_&yr= d2_sdis;

            d2cabg_&yr= d2_cabg;
            d2ptca_&yr= d2_ptca;
            d2cc_&yr  = d2_cc;
            d2ce_&yr  = d2_ce;
            d2rff_&yr = d2_rff;
            d2thr_&yr = d2_thr;
            d2tkr_&yr = d2_tkr;
            d2xli_&yr = d2_xli;
            d2oc_&yr  = d2_oc;
            d2clc_&yr = d2_clc;
            d2ah_&yr  = d2_ah;
            d2vh_&yr  = d2_vh;
            d2p_&yr   = d2_p;
            d2mast_&yr= d2_mast;
            d2lump_&yr= d2_lump;
            d2bs_&yr  = d2_bs;
            d2bwls_&yr= d2_bwls;


        *-- Creating Indicator or non-missings --*;

            if bcs_3>0  then inbcs_&yr=1;
            if cmc_3>0  then incmc_&yr=1;
            if cdc1_3>0 then incdc1_&yr=1;
            if cdc3_3>0 then incdc3_&yr=1;
            if cdc4_3>0 then incdc4_&yr=1;
            if gso_3>0  then ingso_&yr=1;

            if opv>0 then inopv_&yr=1;
            if erv>0 then inerv_&yr=1;
            if asp>0 then inasp_&yr=1;
            if ors>0 then inors_&yr=1;

            if tday>0 then intday_&yr=1;
            if mday>0 then inmday_&yr=1;
            if sday>0 then insday_&yr=1;
            if tdis>0 then intdis_&yr=1;
            if mdis>0 then inmdis_&yr=1;
            if sdis>0 then insdis_&yr=1;
 
            if cabg>0 then incabg_&yr =1;
            if ptca>0 then inptca_&yr =1;
            if cc>0   then incc_&yr   =1;
            if ce>0   then ince_&yr   =1;
            if rff>0  then inrff_&yr  =1;
            if thr>0  then inthr_&yr  =1;
            if tkr>0  then intkr_&yr  =1;
            if xli>0  then inxli_&yr  =1;
            if oc>0   then inoc_&yr   =1;
            if clc>0  then inclc_&yr  =1;
            if ah>0   then inah_&yr   =1;
            if vh>0   then invh_&yr   =1;
            if p>0    then inp_&yr    =1;
            if mast>0 then inmast_&yr =1;
            if lump>0 then inlump_&yr =1;
            if bs>0   then inbs_&yr   =1;
            if bwls>0 then inbwls_&yr =1;

            proc sort; by org_id;

       
       data &outdat; merge b0 b0r c01; by org_id;

             ** Initial Rule 3 Exclusions **;

             if bcsr>0  and abs(bcsr  - mean_bcsr) >= 2*gstd_bcsr  then i_bcsr_&yr=1;
             if cmcr>0  and abs(cmcr  - mean_cmcr) >= 2*gstd_cmcr  then i_cmcr_&yr=1;
             if cdc1r>0 and abs(cdc1r - mean_cdc1r)>= 2*gstd_cdc1r then i_cdc1r_&yr=1;
             if cdc3r>0 and abs(cdc3r - mean_cdc3r)>= 2*gstd_cdc3r then i_cdc3r_&yr=1;
             if cdc4r>0 and abs(cdc4r - mean_cdc4r)>= 2*gstd_cdc4r then i_cdc4r_&yr=1;
             if gsor>0  and abs(gsor  - mean_gsor) >= 2*gstd_gsor  then i_gsor_&yr=1;

             if opvr>0 and abs(opvr - mean_opvr) >= 2*gstd_opvr then i_opvr_&yr=1; 
             if ervr>0 and abs(ervr - mean_ervr) >= 2*gstd_ervr then i_ervr_&yr=1; 
             if aspr>0 and abs(aspr - mean_aspr) >= 2*gstd_aspr then i_aspr_&yr=1; 
             if orsr>0 and abs(orsr - mean_orsr) >= 2*gstd_orsr then i_orsr_&yr=1; 

             if tdayr>0 and abs(tdayr - mean_tdayr) >= 2*gstd_tdayr then i_tdayr_&yr=1; 
             if mdayr>0 and abs(mdayr - mean_mdayr) >= 2*gstd_mdayr then i_mdayr_&yr=1; 
             if sdayr>0 and abs(sdayr - mean_sdayr) >= 2*gstd_sdayr then i_sdayr_&yr=1; 
             if tdisr>0 and abs(tdisr - mean_tdisr) >= 2*gstd_tdisr then i_tdisr_&yr=1; 
             if mdisr>0 and abs(mdisr - mean_mdisr) >= 2*gstd_mdisr then i_mdisr_&yr=1; 
             if sdisr>0 and abs(sdisr - mean_sdisr) >= 2*gstd_sdisr then i_sdisr_&yr=1; 

             if cabgr>0 and abs(cabgr - mean_cabgr) >= 2*gstd_cabgr then i_cabgr_&yr=1;
             if ptcar>0 and abs(ptcar - mean_ptcar) >= 2*gstd_ptcar then i_ptcar_&yr=1;
             if ccr>0   and abs(ccr   - mean_ccr)   >= 2*gstd_ccr   then i_ccr_&yr=1;
             if cer>0   and abs(cer   - mean_cer)   >= 2*gstd_cer   then i_cer_&yr=1;
             if rffr>0  and abs(rffr  - mean_rffr)  >= 2*gstd_rffr  then i_rffr_&yr=1;
             if thrr>0  and abs(thrr  - mean_thrr)  >= 2*gstd_thrr  then i_thrr_&yr=1;
             if tkrr>0  and abs(tkrr  - mean_tkrr)  >= 2*gstd_tkrr  then i_tkrr_&yr=1;
             if xlir>0  and abs(xlir  - mean_xlir)  >= 2*gstd_xlir  then i_xlir_&yr=1;
             if ocr>0   and abs(ocr   - mean_ocr)   >= 2*gstd_ocr   then i_ocr_&yr=1;
             if clcr>0  and abs(clcr  - mean_clcr)  >= 2*gstd_clcr  then i_clcr_&yr=1;
             if ahr>0   and abs(ahr   - mean_ahr)   >= 2*gstd_ahr   then i_ahr_&yr=1;
             if vhr>0   and abs(vhr   - mean_vhr)   >= 2*gstd_vhr   then i_vhr_&yr=1;
             if pr>0    and abs(pr    - mean_pr)    >= 2*gstd_pr    then i_pr_&yr=1;
             if mastr>0 and abs(mastr - mean_mastr) >= 2*gstd_mastr then i_mastr_&yr=1;
             if lumpr>0 and abs(lumpr - mean_lumpr) >= 2*gstd_lumpr then i_lumpr_&yr=1;
             if bsr>0   and abs(bsr   - mean_bsr)   >= 2*gstd_bsr   then i_bsr_&yr=1;
             if bwls>0  and abs(bwlsr - mean_bwlsr) >= 2*gstd_bwlsr then i_bwls_&yr=1;

  %mend;

  %comb2(comb0,b03,2003,03);
  %comb2(comb0,b04,2004,04);
  %comb2(comb0,b05,2005,05);
  %comb2(comb0,b06,2006,06);
  %comb2(comb0,b07,2007,07);
  %comb2(comb0,b08,2008,08);
  %comb2(comb0,b09,2009,09);
  %comb2(comb0,b10,2010,10);
  %comb2(comb0,b11,2011,11);
  %comb2(comb0,b12,2012,12);

  data comb2; merge b03 b04 b05 b06 b07 b08 b09 b10 b11 b12; by org_id;

       *--------------------------------------------------------------*;
       * Create Indicators for any plan has had any initial exclusion *;
       *--------------------------------------------------------------*;

       ind_bcsr =sum(i_bcsr_03,i_bcsr_04,i_bcsr_05,i_bcsr_06,i_bcsr_07,i_bcsr_08,i_bcsr_09,i_bcsr_10,i_bcsr_11,i_bcsr_12);
       ind_cmcr =sum(i_cmcr_03,i_cmcr_04,i_cmcr_05,i_cmcr_06,i_cmcr_07,i_cmcr_08,i_cmcr_09,i_cmcr_10,i_cmcr_11,i_cmcr_12);
       ind_cdc1r=sum(i_cdc1r_03,i_cdc1r_04,i_cdc1r_05,i_cdc1r_06,i_cdc1r_07,i_cdc1r_08,i_cdc1r_09,i_cdc1r_10,i_cdc1r_11,i_cdc1r_12);
       ind_cdc3r=sum(i_cdc3r_03,i_cdc3r_04,i_cdc3r_05,i_cdc3r_06,i_cdc3r_07,i_cdc3r_08,i_cdc3r_09,i_cdc3r_10,i_cdc3r_11,i_cdc3r_12);
       ind_cdc4r=sum(i_cdc4r_03,i_cdc4r_04,i_cdc4r_05,i_cdc4r_06,i_cdc4r_07,i_cdc4r_08,i_cdc4r_09,i_cdc4r_10,i_cdc4r_11,i_cdc4r_12);
       ind_gsor =sum(i_gsor_03,i_gsor_04,i_gsor_05,i_gsor_06,i_gsor_07,i_gsor_08,i_gsor_09,i_gsor_10,i_gsor_11,i_gsor_12);

       ind_opvr=sum(i_opvr_03,i_opvr_04,i_opvr_05,i_opvr_06,i_opvr_07,i_opvr_08,i_opvr_09,i_opvr_10,i_opvr_11,i_opvr_12);
       ind_ervr=sum(i_ervr_03,i_ervr_04,i_ervr_05,i_ervr_06,i_ervr_07,i_ervr_08,i_ervr_09,i_ervr_10,i_ervr_11,i_ervr_12);
       ind_aspr=sum(i_aspr_03,i_aspr_04,i_aspr_05,i_aspr_06,i_aspr_07,i_aspr_08,i_aspr_09,i_aspr_10,i_aspr_11,i_aspr_12);
       ind_orsr=sum(i_orsr_03,i_orsr_04,i_orsr_05,i_orsr_06,i_orsr_07,i_orsr_08,i_orsr_09,i_orsr_10,i_orsr_11,i_orsr_12);

       ind_tdayr=sum(i_tdayr_03,i_tdayr_04,i_tdayr_05,i_tdayr_06,i_tdayr_07,i_tdayr_08,i_tdayr_09,i_tdayr_10,i_tdayr_11,i_tdayr_12);
       ind_mdayr=sum(i_mdayr_03,i_mdayr_04,i_mdayr_05,i_mdayr_06,i_mdayr_07,i_mdayr_08,i_mdayr_09,i_mdayr_10,i_mdayr_11,i_mdayr_12);
       ind_sdayr=sum(i_sdayr_03,i_sdayr_04,i_sdayr_05,i_sdayr_06,i_sdayr_07,i_sdayr_08,i_sdayr_09,i_sdayr_10,i_sdayr_11,i_sdayr_12);
       ind_tdisr=sum(i_tdisr_03,i_tdisr_04,i_tdisr_05,i_tdisr_06,i_tdisr_07,i_tdisr_08,i_tdisr_09,i_tdisr_10,i_tdisr_11,i_tdisr_12);
       ind_mdisr=sum(i_mdisr_03,i_mdisr_04,i_mdisr_05,i_mdisr_06,i_mdisr_07,i_mdisr_08,i_mdisr_09,i_mdisr_10,i_mdisr_11,i_mdisr_12);
       ind_sdisr=sum(i_sdisr_03,i_sdisr_04,i_sdisr_05,i_sdisr_06,i_sdisr_07,i_sdisr_08,i_sdisr_09,i_sdisr_10,i_sdisr_11,i_sdisr_12);

       ind_cabgr=sum(i_cabgr_03,i_cabgr_04,i_cabgr_05,i_cabgr_06,i_cabgr_07,i_cabgr_08,i_cabgr_09,i_cabgr_10,i_cabgr_11,i_cabgr_12);
       ind_ptcar=sum(i_ptcar_03,i_ptcar_04,i_ptcar_05,i_ptcar_06,i_ptcar_07,i_ptcar_08,i_ptcar_09,i_ptcar_10,i_ptcar_11,i_ptcar_12);
       ind_ccr  =sum(i_ccr_03,i_ccr_04,i_ccr_05,i_ccr_06,i_ccr_07,i_ccr_08,i_ccr_09,i_ccr_10,i_ccr_11,i_ccr_12);
       ind_cer  =sum(i_cer_03,i_cer_04,i_cer_05,i_cer_06,i_cer_07,i_cer_08,i_cer_09,i_cer_10,i_cer_11,i_cer_12);
       ind_rffr =sum(i_rffr_03,i_rffr_04,i_rffr_05,i_rffr_06,i_rffr_07,i_rffr_08,i_rffr_09,i_rffr_10,i_rffr_11,i_rffr_12);
       ind_thrr =sum(i_thrr_03,i_thrr_04,i_thrr_05,i_thrr_06,i_thrr_07,i_thrr_08,i_thrr_09,i_thrr_10,i_thrr_11,i_thrr_12);
       ind_tkrr =sum(i_tkrr_03,i_tkrr_04,i_tkrr_05,i_tkrr_06,i_tkrr_07,i_tkrr_08,i_tkrr_09,i_tkrr_10,i_tkrr_11,i_tkrr_12);
       ind_xlir =sum(i_xlir_03,i_xlir_04,i_xlir_05,i_xlir_06,i_xlir_07,i_xlir_08,i_xlir_09,i_xlir_10,i_xlir_11,i_xlir_12);
       ind_ocr  =sum(i_ocr_03,i_ocr_04,i_ocr_05,i_ocr_06,i_ocr_07,i_ocr_08,i_ocr_09,i_ocr_10,i_ocr_11,i_ocr_12);
       ind_clcr =sum(i_clcr_03,i_clcr_04,i_clcr_05,i_clcr_06,i_clcr_07,i_clcr_08,i_clcr_09,i_clcr_10,i_clcr_11,i_clcr_12);
       ind_ahr  =sum(i_ahr_03,i_ahr_04,i_ahr_05,i_ahr_06,i_ahr_07,i_ahr_08,i_ahr_09,i_ahr_10,i_ahr_11,i_ahr_12);
       ind_vhr  =sum(i_vhr_03,i_vhr_04,i_vhr_05,i_vhr_06,i_vhr_07,i_vhr_08,i_vhr_09,i_vhr_10,i_vhr_11,i_vhr_12);
       ind_pr   =sum(i_pr_03,i_pr_04,i_pr_05,i_pr_06,i_pr_07,i_pr_08,i_pr_09,i_pr_10,i_pr_11,i_pr_12);

       ind_mastr=sum(i_mastr_03,i_mastr_04,i_mastr_05,i_mastr_06,i_mastr_07,i_mastr_08,i_mastr_09,i_mastr_10,i_mastr_11,i_mastr_12);
       ind_lumpr=sum(i_lumpr_03,i_lumpr_04,i_lumpr_05,i_lumpr_06,i_lumpr_07,i_lumpr_08,i_lumpr_09,i_lumpr_10,i_lumpr_11,i_lumpr_12);
       ind_bsr  =sum(i_bsr_03,i_bsr_04,i_bsr_05,i_bsr_06,i_bsr_07,i_bsr_08,i_bsr_09,i_bsr_10,i_bsr_11,i_bsr_12);
       ind_bwlsr=sum(i_bwlsr_03,i_bwlsr_04,i_bwlsr_05,i_bwlsr_06,i_bwlsr_07,i_bwlsr_08,i_bwlsr_09,i_bwlsr_10,i_bwlsr_11,i_bwlsr_12);

       ind1_bcs =sum(d1bcs_03,d1bcs_04,d1bcs_05,d1bcs_06,d1bcs_07,d1bcs_08,d1bcs_09,d1bcs_10,d1bcs_11,d1bcs_12);
       ind1_cmc =sum(d1cmc_03,d1cmc_04,d1cmc_05,d1cmc_06,d1cmc_07,d1cmc_08,d1cmc_09,d1cmc_10,d1cmc_11,d1cmc_12);
       ind1_cdc1 =sum(d1cdc1_03,d1cdc1_04,d1cdc1_05,d1cdc1_06,d1cdc1_07,d1cdc1_08,d1cdc1_09,d1cdc1_10,d1cdc1_11,d1cdc1_12);
       ind1_cdc3 =sum(d1cdc3_03,d1cdc3_04,d1cdc3_05,d1cdc3_06,d1cdc3_07,d1cdc3_08,d1cdc3_09,d1cdc3_10,d1cdc3_11,d1cdc3_12);
       ind1_cdc4 =sum(d1cdc4_03,d1cdc4_04,d1cdc4_05,d1cdc4_06,d1cdc4_07,d1cdc4_08,d1cdc4_09,d1cdc4_10,d1cdc4_11,d1cdc4_12);
       ind1_gso =sum(d1gso_03,d1gso_04,d1gso_05,d1gso_06,d1gso_07,d1gso_08,d1gso_09,d1gso_10,d1gso_11,d1gso_12);

       ind1_opv =sum(d1opv_03,d1opv_04,d1opv_05,d1opv_06,d1opv_07,d1opv_08,d1opv_09,d1opv_10,d1opv_11,d1opv_12);
       ind1_erv =sum(d1erv_03,d1erv_04,d1erv_05,d1erv_06,d1erv_07,d1erv_08,d1erv_09,d1erv_10,d1erv_11,d1erv_12);
       ind1_asp =sum(d1asp_03,d1asp_04,d1asp_05,d1asp_06,d1asp_07,d1asp_08,d1asp_09,d1asp_10,d1asp_11,d1asp_12);
       ind1_ors =sum(d1ors_03,d1ors_04,d1ors_05,d1ors_06,d1ors_07,d1ors_08,d1ors_09,d1ors_10,d1ors_11,d1ors_12);

       ind1_tday =sum(d1tday_03,d1tday_04,d1tday_05,d1tday_06,d1tday_07,d1tday_08,d1tday_09,d1tday_10,d1tday_11,d1tday_12);
       ind1_mday =sum(d1mday_03,d1mday_04,d1mday_05,d1mday_06,d1mday_07,d1mday_08,d1mday_09,d1mday_10,d1mday_11,d1mday_12);
       ind1_sday =sum(d1sday_03,d1sday_04,d1sday_05,d1sday_06,d1sday_07,d1sday_08,d1sday_09,d1sday_10,d1sday_11,d1sday_12);
       ind1_tdis =sum(d1tdis_03,d1tdis_04,d1tdis_05,d1tdis_06,d1tdis_07,d1tdis_08,d1tdis_09,d1tdis_10,d1tdis_11,d1tdis_12);
       ind1_mdis =sum(d1mdis_03,d1mdis_04,d1mdis_05,d1mdis_06,d1mdis_07,d1mdis_08,d1mdis_09,d1mdis_10,d1mdis_11,d1mdis_12);
       ind1_sdis =sum(d1sdis_03,d1sdis_04,d1sdis_05,d1sdis_06,d1sdis_07,d1sdis_08,d1sdis_09,d1sdis_10,d1sdis_11,d1sdis_12);

       ind1_cabg =sum(d1cabg_03,d1cabg_04,d1cabg_05,d1cabg_06,d1cabg_07,d1cabg_08,d1cabg_09,d1cabg_10,d1cabg_11,d1cabg_12);
       ind1_ptca =sum(d1ptca_03,d1ptca_04,d1ptca_05,d1ptca_06,d1ptca_07,d1ptca_08,d1ptca_09,d1ptca_10,d1ptca_11,d1ptca_12);
       ind1_cc   =sum(d1cc_03,d1cc_04,d1cc_05,d1cc_06,d1cc_07,d1cc_08,d1cc_09,d1cc_10,d1cc_11,d1cc_12);
       ind1_ce   =sum(d1ce_03,d1ce_04,d1ce_05,d1ce_06,d1ce_07,d1ce_08,d1ce_09,d1ce_10,d1ce_11,d1ce_12);
       ind1_rff =sum(d1rff_03,d1rff_04,d1rff_05,d1rff_06,d1rff_07,d1rff_08,d1rff_09,d1rff_10,d1rff_11,d1rff_12);
       ind1_thr =sum(d1thr_03,d1thr_04,d1thr_05,d1thr_06,d1thr_07,d1thr_08,d1thr_09,d1thr_10,d1thr_11,d1thr_12);
       ind1_tkr =sum(d1tkr_03,d1tkr_04,d1tkr_05,d1tkr_06,d1tkr_07,d1tkr_08,d1tkr_09,d1tkr_10,d1tkr_11,d1thr_12);
       ind1_xli =sum(d1xli_03,d1xli_04,d1xli_05,d1xli_06,d1xli_07,d1xli_08,d1xli_09,d1xli_10,d1xli_11,d1xli_12);
       ind1_oc  =sum(d1oc_03,d1oc_04,d1oc_05,d1oc_06,d1oc_07,d1oc_08,d1oc_09,d1oc_10,d1oc_11,d1oc_12);
       ind1_clc =sum(d1clc_03,d1clc_04,d1clc_05,d1clc_06,d1clc_07,d1clc_08,d1clc_09,d1clc_10,d1clc_11,d1clc_12);
       ind1_ah  =sum(d1ah_03,d1ah_04,d1ah_05,d1ah_06,d1ah_07,d1ah_08,d1ah_09,d1ah_10,d1ah_11,d1ah_12);
       ind1_vh  =sum(d1vh_03,d1vh_04,d1vh_05,d1vh_06,d1vh_07,d1vh_08,d1vh_09,d1vh_10,d1vh_11,d1vh_12);
       ind1_p  =sum(d1p_03,d1p_04,d1p_05,d1p_06,d1p_07,d1p_08,d1p_09,d1p_10,d1p_11,d1p_12);
       ind1_mast =sum(d1mast_03,d1mast_04,d1mast_05,d1mast_06,d1mast_07,d1mast_08,d1mast_09,d1mast_10,d1mast_11,d1mast_12);
       ind1_lump =sum(d1lump_03,d1lump_04,d1lump_05,d1lump_06,d1lump_07,d1lump_08,d1lump_09,d1lump_10,d1lump_11,d1lump_12);
       ind1_bs  =sum(d1bs_03,d1bs_04,d1bs_05,d1bs_06,d1bs_07,d1bs_08,d1bs_09,d1bs_10,d1bs_11,d1bs_12);
       ind1_bwls=sum(d1bwls_03,d1bwls_04,d1bwls_05,d1bwls_06,d1bwls_07,d1bwls_08,d1bwls_09,d1bwls_10,d1bwls_11,d1bwls_12);

      ind2_bcs =sum(d2bcs_03,d2bcs_04,d2bcs_05,d2bcs_06,d2bcs_07,d2bcs_08,d2bcs_09,d2bcs_10,d2bcs_11,d2bcs_12);
       ind2_cmc =sum(d2cmc_03,d2cmc_04,d2cmc_05,d2cmc_06,d2cmc_07,d2cmc_08,d2cmc_09,d2cmc_10,d2cmc_11,d2cmc_12);
       ind2_cdc1 =sum(d2cdc1_03,d2cdc1_04,d2cdc1_05,d2cdc1_06,d2cdc1_07,d2cdc1_08,d2cdc1_09,d2cdc1_10,d2cdc1_11,d2cdc1_12);
       ind2_cdc3 =sum(d2cdc3_03,d2cdc3_04,d2cdc3_05,d2cdc3_06,d2cdc3_07,d2cdc3_08,d2cdc3_09,d2cdc3_10,d2cdc3_11,d2cdc3_12);
       ind2_cdc4 =sum(d2cdc4_03,d2cdc4_04,d2cdc4_05,d2cdc4_06,d2cdc4_07,d2cdc4_08,d2cdc4_09,d2cdc4_10,d2cdc4_11,d2cdc4_12);
       ind2_gso =sum(d2gso_03,d2gso_04,d2gso_05,d2gso_06,d2gso_07,d2gso_08,d2gso_09,d2gso_10,d2gso_11,d2gso_12);

       ind2_opv =sum(d2opv_03,d2opv_04,d2opv_05,d2opv_06,d2opv_07,d2opv_08,d2opv_09,d2opv_10,d2opv_11,d2opv_12);
       ind2_erv =sum(d2erv_03,d2erv_04,d2erv_05,d2erv_06,d2erv_07,d2erv_08,d2erv_09,d2erv_10,d2erv_11,d2erv_12);
       ind2_asp =sum(d2asp_03,d2asp_04,d2asp_05,d2asp_06,d2asp_07,d2asp_08,d2asp_09,d2asp_10,d2asp_11,d2asp_12);
       ind2_ors =sum(d2ors_03,d2ors_04,d2ors_05,d2ors_06,d2ors_07,d2ors_08,d2ors_09,d2ors_10,d2ors_11,d2ors_12);

       ind2_tday =sum(d2tday_03,d2tday_04,d2tday_05,d2tday_06,d2tday_07,d2tday_08,d2tday_09,d2tday_10,d2tday_11,d2tday_12);
       ind2_mday =sum(d2mday_03,d2mday_04,d2mday_05,d2mday_06,d2mday_07,d2mday_08,d2mday_09,d2mday_10,d2mday_11,d2mday_12);
       ind2_sday =sum(d2sday_03,d2sday_04,d2sday_05,d2sday_06,d2sday_07,d2sday_08,d2sday_09,d2sday_10,d2sday_11,d2sday_12);
       ind2_tdis =sum(d2tdis_03,d2tdis_04,d2tdis_05,d2tdis_06,d2tdis_07,d2tdis_08,d2tdis_09,d2tdis_10,d2tdis_11,d2tdis_12);
       ind2_mdis =sum(d2mdis_03,d2mdis_04,d2mdis_05,d2mdis_06,d2mdis_07,d2mdis_08,d2mdis_09,d2mdis_10,d2mdis_11,d2mdis_12);
       ind2_sdis =sum(d2sdis_03,d2sdis_04,d2sdis_05,d2sdis_06,d2sdis_07,d2sdis_08,d2sdis_09,d2sdis_10,d2sdis_11,d2sdis_12);

       ind2_cabg =sum(d2cabg_03,d2cabg_04,d2cabg_05,d2cabg_06,d2cabg_07,d2cabg_08,d2cabg_09,d2cabg_10,d2cabg_11,d2cabg_12);
       ind2_ptca =sum(d2ptca_03,d2ptca_04,d2ptca_05,d2ptca_06,d2ptca_07,d2ptca_08,d2ptca_09,d2ptca_10,d2ptca_11,d2ptca_12);
       ind2_cc   =sum(d2cc_03,d2cc_04,d2cc_05,d2cc_06,d2cc_07,d2cc_08,d2cc_09,d2cc_10,d2cc_11,d2cc_12);
       ind2_ce   =sum(d2ce_03,d2ce_04,d2ce_05,d2ce_06,d2ce_07,d2ce_08,d2ce_09,d2ce_10,d2ce_11,d2ce_12);
       ind2_rff =sum(d2rff_03,d2rff_04,d2rff_05,d2rff_06,d2rff_07,d2rff_08,d2rff_09,d2rff_10,d2rff_11,d2rff_12);
       ind2_thr =sum(d2thr_03,d2thr_04,d2thr_05,d2thr_06,d2thr_07,d2thr_08,d2thr_09,d2thr_10,d2thr_11,d2thr_12);
       ind2_tkr =sum(d2tkr_03,d2tkr_04,d2tkr_05,d2tkr_06,d2tkr_07,d2tkr_08,d2tkr_09,d2tkr_10,d2tkr_11,d2thr_12);
       ind2_xli =sum(d2xli_03,d2xli_04,d2xli_05,d2xli_06,d2xli_07,d2xli_08,d2xli_09,d2xli_10,d2xli_11,d2xli_12);
       ind2_oc  =sum(d2oc_03,d2oc_04,d2oc_05,d2oc_06,d2oc_07,d2oc_08,d2oc_09,d2oc_10,d2oc_11,d2oc_12);
       ind2_clc =sum(d2clc_03,d2clc_04,d2clc_05,d2clc_06,d2clc_07,d2clc_08,d2clc_09,d2clc_10,d2clc_11,d2clc_12);
       ind2_ah  =sum(d2ah_03,d2ah_04,d2ah_05,d2ah_06,d2ah_07,d2ah_08,d2ah_09,d2ah_10,d2ah_11,d2ah_12);
       ind2_vh  =sum(d2vh_03,d2vh_04,d2vh_05,d2vh_06,d2vh_07,d2vh_08,d2vh_09,d2vh_10,d2vh_11,d2vh_12);
       ind2_p  =sum(d2p_03,d2p_04,d2p_05,d2p_06,d2p_07,d2p_08,d2p_09,d2p_10,d2p_11,d2p_12);
       ind2_mast =sum(d2mast_03,d2mast_04,d2mast_05,d2mast_06,d2mast_07,d2mast_08,d2mast_09,d2mast_10,d2mast_11,d2mast_12);
       ind2_lump =sum(d2lump_03,d2lump_04,d2lump_05,d2lump_06,d2lump_07,d2lump_08,d2lump_09,d2lump_10,d2lump_11,d2lump_12);
       ind2_bs  =sum(d2bs_03,d2bs_04,d2bs_05,d2bs_06,d2bs_07,d2bs_08,d2bs_09,d2bs_10,d2bs_11,d2bs_12);
       ind2_bwls=sum(d2bwls_03,d2bwls_04,d2bwls_05,d2bwls_06,d2bwls_07,d2bwls_08,d2bwls_09,d2bwls_10,d2bwls_11,d2bwls_12);

       numyr_bcs =sum(inbcs_03,inbcs_04,inbcs_05,inbcs_06,inbcs_07,inbcs_08,inbcs_09,inbcs_10,inbcs_11,inbcs_12);
       numyr_cmc =sum(incmc_03,incmc_04,incmc_05,incmc_06,incmc_07,incmc_08,incmc_09,incmc_10,incmc_11,incmc_12);
       numyr_cdc1 =sum(incdc1_03,incdc1_04,incdc1_05,incdc1_06,incdc1_07,incdc1_08,incdc1_09,incdc1_10,incdc1_11,incdc1_12);
       numyr_cdc3 =sum(incdc3_03,incdc3_04,incdc3_05,incdc3_06,incdc3_07,incdc3_08,incdc3_09,incdc3_10,incdc3_11,incdc3_12);
       numyr_cdc4 =sum(incdc4_03,incdc4_04,incdc4_05,incdc4_06,incdc4_07,incdc4_08,incdc4_09,incdc4_10,incdc4_11,incdc4_12);
       numyr_gso =sum(ingso_03,ingso_04,ingso_05,ingso_06,ingso_07,ingso_08,ingso_09,ingso_10,ingso_11,ingso_12);

       numyr_opv =sum(inopv_03,inopv_04,inopv_05,inopv_06,inopv_07,inopv_08,inopv_09,inopv_10,inopv_11,inopv_12);
       numyr_erv =sum(inerv_03,inerv_04,inerv_05,inerv_06,inerv_07,inerv_08,inerv_09,inerv_10,inerv_11,inerv_12);
       numyr_asp =sum(inasp_03,inasp_04,inasp_05,inasp_06,inasp_07,inasp_08,inasp_09,inasp_10,inasp_11,inasp_12);
       numyr_ors =sum(inors_03,inors_04,inors_05,inors_06,inors_07,inors_08,inors_09,inors_10,inors_11,inors_12);

       numyr_tday =sum(intday_03,intday_04,intday_05,intday_06,intday_07,intday_08,intday_09,intday_10,intday_11,intday_12);
       numyr_mday =sum(inmday_03,inmday_04,inmday_05,inmday_06,inmday_07,inmday_08,inmday_09,inmday_10,inmday_11,inmday_12);
       numyr_sday =sum(insday_03,insday_04,insday_05,insday_06,insday_07,insday_08,insday_09,insday_10,insday_11,insday_12);
       numyr_tdis =sum(intdis_03,intdis_04,intdis_05,intdis_06,intdis_07,intdis_08,intdis_09,intdis_10,intdis_11,intdis_12);
       numyr_mdis =sum(inmdis_03,inmdis_04,inmdis_05,inmdis_06,inmdis_07,inmdis_08,inmdis_09,inmdis_10,inmdis_11,inmdis_12);
       numyr_sdis =sum(insdis_03,insdis_04,insdis_05,insdis_06,insdis_07,insdis_08,insdis_09,insdis_10,insdis_11,insdis_12);

       numyr_cabg =sum(incabg_03,incabg_04,incabg_05,incabg_06,incabg_07,incabg_08,incabg_09,incabg_10,incabg_11,incabg_12);
       numyr_ptca =sum(inptca_03,inptca_04,inptca_05,inptca_06,inptca_07,inptca_08,inptca_09,inptca_10,inptca_11,inptca_12);
       numyr_cc   =sum(incc_03,incc_04,incc_05,incc_06,incc_07,incc_08,incc_09,incc_10,incc_11,incc_12);
       numyr_ce   =sum(ince_03,ince_04,ince_05,ince_06,ince_07,ince_08,ince_09,ince_10,ince_11,ince_12);
       numyr_rff =sum(inrff_03,inrff_04,inrff_05,inrff_06,inrff_07,inrff_08,inrff_09,inrff_10,inrff_11,inrff_12);
       numyr_thr =sum(inthr_03,inthr_04,inthr_05,inthr_06,inthr_07,inthr_08,inthr_09,inthr_10,inthr_11,inthr_12);
       numyr_tkr =sum(intkr_03,intkr_04,intkr_05,intkr_06,intkr_07,intkr_08,intkr_09,intkr_10,intkr_11,inthr_12);
       numyr_xli =sum(inxli_03,inxli_04,inxli_05,inxli_06,inxli_07,inxli_08,inxli_09,inxli_10,inxli_11,inxli_12);
       numyr_oc  =sum(inoc_03,inoc_04,inoc_05,inoc_06,inoc_07,inoc_08,inoc_09,inoc_10,inoc_11,inoc_12);
       numyr_clc =sum(inclc_03,inclc_04,inclc_05,inclc_06,inclc_07,inclc_08,inclc_09,inclc_10,inclc_11,inclc_12);
       numyr_ah  =sum(inah_03,inah_04,inah_05,inah_06,inah_07,inah_08,inah_09,inah_10,inah_11,inah_12);
       numyr_vh  =sum(invh_03,invh_04,invh_05,invh_06,invh_07,invh_08,invh_09,invh_10,invh_11,invh_12);
       numyr_p  =sum(inp_03,inp_04,inp_05,inp_06,inp_07,inp_08,inp_09,inp_10,inp_11,inp_12);
       numyr_mast =sum(inmast_03,inmast_04,inmast_05,inmast_06,inmast_07,inmast_08,inmast_09,inmast_10,inmast_11,inmast_12);
       numyr_lump =sum(inlump_03,inlump_04,inlump_05,inlump_06,inlump_07,inlump_08,inlump_09,inlump_10,inlump_11,inlump_12);
       numyr_bs  =sum(inbs_03,inbs_04,inbs_05,inbs_06,inbs_07,inbs_08,inbs_09,inbs_10,inbs_11,inbs_12);
       numyr_bwls=sum(inbwls_03,inbwls_04,inbwls_05,inbwls_06,inbwls_07,inbwls_08,inbwls_09,inbwls_10,inbwls_11,inbwls_12);

       proc sort; by org_id;

   
    **********************************************************************;
    ** Following Steps Do 2 interactions for further exclusion of Plans **;
    ** Using larger than 2 grand SD rule each time recalculate mean     **;
    **********************************************************************;
  
   %macro sub(year,yr,outdat);

        data a0; set comb2;

            year = &year;

            bcsr  =  bcsr_&yr;
            cmcr  =  cmcr_&yr;
            cdc1r = cdc1r_&yr;
            cdc3r = cdc3r_&yr;
            cdc4r = cdc4r_&yr;
            gsor  =  gsor_&yr;

            opvr  = opvr_&yr;
            ervr  = ervr_&yr;
            aspr  = aspr_&yr;
            orsr  = orsr_&yr;

            tdayr =  tdayr_&yr;
            mdayr =  mdayr_&yr;
            sdayr =  sdayr_&yr;
            tdisr =  tdisr_&yr;
            mdisr =  mdisr_&yr;
            sdisr =  sdisr_&yr;

            cabgr = cabgr_&yr;
            ptcar = ptcar_&yr;
            ccr   = ccr_&yr;
            cer   = cer_&yr;
            rffr  = rffr_&yr;
            thrr  = thrr_&yr;
            tkrr  = tkrr_&yr;
            xlir  = xlir_&yr;
            ocr   = ocr_&yr;
            clcr  = clcr_&yr;
            ahr   = ahr_&yr;
            vhr   = vhr_&yr;
            pr    = pr_&yr;
            mastr = mastr_&yr;
            lumpr = lumpr_&yr;
	    bsr   = bsr_&yr;
            bwlsr = bwlsr_&yr;

            i_bcsr  = i_bcsr_&yr;
            i_cmcr  = i_cmcr_&yr;
            i_cdc1r = i_cdc1r_&yr;
            i_cdc3r = i_cdc3r_&yr;
            i_cdc4r = i_cdc4r_&yr;
            i_gsor  = i_gsor_&yr;

            i_opvr  = i_opvr_&yr;
            i_ervr  = i_ervr_&yr;
            i_aspr  = i_aspr_&yr;
            i_orsr  = i_orsr_&yr;

            i_tdayr = i_tdayr_&yr;
            i_mdayr = i_mdayr_&yr;
            i_sdayr = i_sdayr_&yr;
            i_tdisr = i_tdisr_&yr;
            i_mdisr = i_mdisr_&yr;
            i_sdisr = i_sdisr_&yr;

            i_cabgr = i_cabgr_&yr;
            i_ptcar = i_ptcar_&yr;
            i_ccr   = i_ccr_&yr;
            i_cer   = i_cer_&yr;
            i_rffr  = i_rffr_&yr;
            i_thrr  = i_thrr_&yr;
            i_tkrr  = i_tkrr_&yr;
            i_xlir  = i_xlir_&yr;
            i_ocr   = i_ocr_&yr;
            i_clcr  = i_clcr_&yr;
            i_ahr   = i_ahr_&yr;
            i_vhr   = i_vhr_&yr;
            i_pr    = i_pr_&yr;
            i_mastr = i_mastr_&yr;
            i_lumpr = i_lumpr_&yr;
            i_bsr   = i_bsr_&yr;
            i_bwlsr = i_bwlsr_&yr;

         data &outdat; set a0;
     
             %let inds =  ind_bcsr  ind_cmcr  ind_cdc1r ind_cdc3r ind_cdc4r ind_gsor
                          ind_opvr  ind_ervr  ind_aspr  ind_orsr  ind_tdayr ind_mdayr ind_sdayr ind_tdisr ind_mdisr ind_sdisr
                          ind_cabgr ind_ptcar ind_ccr   ind_cer   ind_rffr  ind_thrr  ind_tkrr  ind_xlir  ind_ocr   ind_clcr
                          ind_ahr   ind_vhr   ind_pr    ind_mastr ind_lumpr ind_bsr   ind_bwlsr;

              %let means = mean_bcsr  mean_cmcr  mean_cdc1r mean_cdc3r mean_cdc4r mean_gsor
                           mean_opvr  mean_ervr  mean_aspr  mean_orsr  mean_tdayr mean_mdayr mean_sdayr mean_tdisr mean_mdisr mean_sdisr
                           mean_cabgr mean_ptcar mean_ccr   mean_cer   mean_rffr  mean_thrr  mean_tkrr  mean_xlir  mean_ocr   mean_clcr
                           mean_ahr   mean_vhr   mean_pr    mean_mastr mean_lumpr mean_bsr   mean_bwlsr ;

              %let gstds = gstd_bcsr  gstd_cmcr  gstd_cdc1r gstd_cdc3r gstd_cdc4r gstd_gsor
                           gstd_opvr  gstd_ervr  gstd_aspr  gstd_orsr  gstd_tdayr gstd_mdayr gstd_sdayr gstd_tdisr gstd_mdisr gstd_sdisr
                           gstd_cabgr gstd_ptcar gstd_ccr   gstd_cer   gstd_rffr  gstd_thrr  gstd_tkrr  gstd_xlir  gstd_ocr   gstd_clcr
                           gstd_ahr   gstd_vhr   gstd_pr    gstd_mastr gstd_lumpr gstd_bsr   gstd_bwlsr;
       
              %let numyr = numyr_bcs  numyr_cmc  numyr_cdc1 numyr_cdc3 numyr_cdc4 numyr_gso
                           numyr_opv  numyr_erv  numyr_asp  numyr_ors  numyr_tday numyr_mday numyr_sday numyr_tdis numyr_mdis numyr_sdis
                           numyr_cabg numyr_ptca numyr_cc   numyr_ce   numyr_rff  numyr_thr  numyr_tkr  numyr_xli  numyr_oc   numyr_clc
                           numyr_ah   numyr_vh   numyr_p    numyr_mast numyr_lump numyr_bs   numyr_bwls;

              %let vars  = bcsr   cmcr   cdc1r  cdc3r  cdc4r  gsor
                           opvr   ervr   aspr   orsr   tdayr  mdayr  sdayr  tdisr  mdisr  sdisr
                           cabgr  ptcar  ccr    cer    rffr   thrr   tkrr   xlir   ocr    clcr
                           ahr    vhr    pr     mastr  lumpr  bsr    bwlsr;

              %let vars_i= i_bcsr  i_cmcr  i_cdc1r i_cdc3r i_cdc4r i_gsor
                           i_opvr  i_ervr  i_aspr  i_orsr  i_tdayr i_mdayr i_sdayr i_tdisr i_mdisr i_sdisr
                           i_cabgr i_ptcar i_ccr   i_cer   i_rffr  i_thrr  i_tkrr  i_xlir  i_ocr   i_clcr
                           i_ahr   i_vhr   i_pr    i_mastr i_lumpr i_bsr   i_bwlsr ;

             keep org_id year &inds &means &gstds &numyr &vars &vars_i;

      %mend;

      %sub(2003,03,s03);
      %sub(2004,04,s04);
      %sub(2005,05,s05);
      %sub(2006,06,s06);
      %sub(2007,07,s07);
      %sub(2008,08,s08);
      %sub(2009,09,s09);
      %sub(2010,10,s10);
      %sub(2011,11,s11);
      %sub(2012,12,s12);

    data sub1; set s03 s04 s05 s06 s07 s08 s09 s10 s11 s12;


         *** Starting Macro For 2 Iterations ***;

       %macro iter(var,var2);

          data one; set sub1;
               
               if ind_&var >=2 and numyr_&var2 >2 ;

               d_&var = abs(&var - mean_&var);
               proc sort; by org_id year;
        
            data keepone; set one;

                 if i_&var=1;
                 i2max_&var=1; 
                 i2_&var=1;
                 keep i_&var i2max_&var org_id year d_&var;

                 proc sort; by org_id descending d_&var;
                 proc sort nodupkey; by org_id;   /** one entry per Plan **/
                 proc sort; by org_id year;

            data two01; merge one keepone; by org_id year;    
      
                 proc means mean data=two01(where=(i2max_&var=.)) noprint nway;
                      class org_id;
                      var &var ;
                      output out=m2 mean=mean2_&var ;

            data two02(drop=_FREQ_ _TYPE_); merge two01 m2; by org_id;
     
               if &var>0 then d2_&var = abs(&var -mean2_&var);
               if d2_&var > 2*gstd_&var then i2_&var=1;
               proc sort; by org_id;

              proc means sum noprint data=two02 nway;
                   class org_id; 
                   var   i2_&var;
                   output out=cnt02 sum=ind2_&var ;
 
          data two(drop=_FREQ_ _TYPE_); merge two02 cnt02; by org_id;

               data sub02; set two;

                    if ind2_&var < 2; 
                    
                    i3_&var = i2_&var ;
          
           data three01; set two;

                if ind2_&var>=2;
                proc sort; by org_id year;

             data keeptwo; set three01;

                  if i2_&var = 1;
                  if i2max_&var = .;
                  i3max_&var = 1 ; 
                  i3_&var= 1;
                  keep i2_&var i2max_&var i3max_&var org_id year d2_&var;
                  proc sort; by org_id descending d2_&var;
                  proc sort nodupkey; by org_id;   /** one entry per Plan **/
                  proc sort; by org_id year;

           data three02; merge three01 keeptwo; by org_id year;
 
                proc means mean data=three02(where=(i2max_&var=. and i3max_&var=.)) noprint nway;
                     class org_id;
                     var   &var;
                     output out=m3 mean=mean3_&var;

           data three(drop=_FREQ_ _TYPE_); merge three02 m3; by org_id;

                if &var  > 0 then d3_&var = abs(&var -mean3_&var);
                if d3_&var > 2*gstd_&var then i3_&var = 1;
                if i2max_&var = 1 then i3_&var=1;
        
           data s03_&var; set sub02 three;
            
                keep org_id year mean2_&var mean3_&var d_&var d2_&var d3_&var  i3_&var; 
                proc sort; by org_id year;   

    %mend;

    %iter(bcsr,bcs);
    %iter(cmcr,cmc);
    %iter(cdc1r,cdc1);
    %iter(cdc3r,cdc3);
    %iter(cdc4r,cdc4);
    %iter(gsor,gso);
    %iter(opvr,opv);
    %iter(ervr,erv);
    %iter(aspr,asp);
    %iter(orsr,ors);
    %iter(tdayr,tday);
    %iter(mdayr,mday);
    %iter(sdayr,sday);
    %iter(tdisr,tdis);
    %iter(mdisr,mdis);
    %iter(sdisr,sdis);
    %iter(cabgr,cabg); 
    %iter(ptcar,ptca); 
    %iter(ccr,cc);   
    %iter(cer,ce);   
    %iter(rffr,rff);  
    %iter(thrr,thr);  
    %iter(tkrr,tkr);  
    %iter(xlir,xli);  
    %iter(ocr,oc);   
    %iter(clcr,clc);
    %iter(ahr,ah);   
    %iter(vhr,vh);   
    %iter(pr,p);    
    %iter(mastr,mast); 
    %iter(lumpr,lump); 
    %iter(bsr,bs);
    %iter(bwlsr,bwls);

    data sub03; 
         merge  s03_bcsr  s03_cmcr  s03_cdc1r s03_cdc3r s03_cdc4r s03_gsor
                s03_opvr  s03_ervr  s03_aspr  s03_orsr  s03_tdayr s03_mdayr s03_sdayr s03_tdisr s03_mdisr s03_sdisr
                s03_cabgr s03_ptcar s03_ccr   s03_cer   s03_rffr  s03_thrr  s03_tkrr  s03_xlir  s03_ocr   s03_clcr
                s03_ahr   s03_vhr   s03_pr    s03_mastr s03_lumpr s03_bsr   s03_bwlsr ;  
         by org_id year;
     
     %macro reset(year,yr,outdat);

        data a; set sub03;
           
             if year=&year;

            i3_bcsr_&yr  = i3_bcsr;
            i3_cmcr_&yr  = i3_cmcr;
            i3_cdc1r_&yr = i3_cdc1r;
            i3_cdc3r_&yr = i3_cdc3r;
            i3_cdc4r_&yr = i3_cdc4r;
            i3_gsor_&yr  = i3_gsor;

            i3_opvr_&yr  = i3_opvr;
            i3_ervr_&yr  = i3_ervr;
            i3_aspr_&yr  = i3_aspr;
            i3_orsr_&yr  = i3_orsr;

            i3_tdayr_&yr = i3_tdayr;
            i3_mdayr_&yr = i3_mdayr;
            i3_sdayr_&yr = i3_sdayr;
            i3_tdisr_&yr = i3_tdisr;
            i3_mdisr_&yr = i3_mdisr;
            i3_sdisr_&yr = i3_sdisr;

            i3_cabgr_&yr = i3_cabgr;
            i3_ptcar_&yr = i3_ptcar;
            i3_ccr_&yr   = i3_ccr;
            i3_cer_&yr   = i3_cer;
            i3_rffr_&yr  = i3_rffr;
            i3_thrr_&yr  = i3_thrr;
            i3_tkrr_&yr  = i3_tkrr;
            i3_xlir_&yr  = i3_xlir;
            i3_ocr_&yr   = i3_ocr;
            i3_clcr_&yr  = i3_clcr;
            i3_ahr_&yr   = i3_ahr;
            i3_vhr_&yr   = i3_vhr;
            i3_pr_&yr    = i3_pr;
            i3_mastr_&yr = i3_mastr;
            i3_lumpr_&yr = i3_lumpr;
            i3_bsr_&yr   = i3_bsr;
            i3_bwlsr_&yr = i3_bwlsr;

       data &outdat; set a;

            proc sort; by org_id;

   %mend;

   %reset(2003,03,r03);
   %reset(2004,04,r04);
   %reset(2005,05,r05);
   %reset(2006,06,r06);
   %reset(2007,07,r07);
   %reset(2008,08,r08);
   %reset(2009,09,r09);
   %reset(2010,10,r10);
   %reset(2011,11,r11);      
   %reset(2012,12,r12);

   data sub02; merge r03 r04 r05 r06 r07 r08 r09 r10 r11 r12; by org_id;

        proc sort; by org_id;   
  
   data comb03; merge comb2 sub02; by org_id;


        %macro seti3(yr,outd);
                               /** for cases no need for further iteraction **/

            data a; set comb03;

              if ind_bcsr=1  or (ind_bcsr=2  and numyr_bcs=2)  then i3_bcsr_&yr  = i_bcsr_&yr;
              if ind_cmcr=1  or (ind_cmcr=2  and numyr_cmc=2)  then i3_cmcr_&yr  = i_cmcr_&yr;
              if ind_cdc1r=1 or (ind_cdc1r=2 and numyr_cdc1=2) then i3_cdc1r_&yr = i_cdc1r_&yr;
              if ind_cdc3r=1 or (ind_cdc3r=2 and numyr_cdc3=2) then i3_cdc3r_&yr = i_cdc3r_&yr;
              if ind_cdc4r=1 or (ind_cdc4r=2 and numyr_cdc4=2) then i3_cdc4r_&yr = i_cdc4r_&yr;
              if ind_gsor=1  or (ind_gsor=2  and numyr_gso=2)  then i3_gsor_&yr  = i_gsor_&yr;
                 
              if ind_opvr=1 or (ind_opvr=2 and numyr_opv=2) then i3_opvr_&yr = i_opvr_&yr;
              if ind_ervr=1 or (ind_ervr=2 and numyr_erv=2) then i3_ervr_&yr = i_ervr_&yr;
              if ind_aspr=1 or (ind_aspr=2 and numyr_asp=2) then i3_aspr_&yr = i_aspr_&yr;
              if ind_orsr=1 or (ind_orsr=2 and numyr_ors=2) then i3_orsr_&yr = i_orsr_&yr;

              if ind_tdayr=1 or (ind_tdayr=2 and numyr_tday=2) then i3_tdayr_&yr = i_tdayr_&yr;
              if ind_mdayr=1 or (ind_mdayr=2 and numyr_mday=2) then i3_mdayr_&yr = i_mdayr_&yr;
              if ind_sdayr=1 or (ind_sdayr=2 and numyr_sday=2) then i3_sdayr_&yr = i_sdayr_&yr;
              if ind_tdisr=1 or (ind_tdisr=2 and numyr_tdis=2) then i3_tdisr_&yr = i_tdisr_&yr;
              if ind_mdisr=1 or (ind_mdisr=2 and numyr_mdis=2) then i3_mdisr_&yr = i_mdisr_&yr;
              if ind_sdisr=1 or (ind_sdisr=2 and numyr_sdis=2) then i3_sdisr_&yr = i_sdisr_&yr;

              if ind_cabgr=1 or (ind_cabgr=2 and numyr_cabg=2) then i3_cabgr_&yr = i_cabgr_&yr;
              if ind_ptcar=1 or (ind_ptcar=2 and numyr_ptca=2) then i3_ptcar_&yr = i_ptcar_&yr;
              if ind_ccr=1   or (ind_ccr=2   and numyr_cc=2)   then i3_ccr_&yr   = i_ccr_&yr;
              if ind_cer=1   or (ind_cer=2   and numyr_ce=2)   then i3_cer_&yr   = i_cer_&yr;
              if ind_rffr=1  or (ind_rffr=2  and numyr_rff=2)  then i3_rffr_&yr  = i_rffr_&yr;
              if ind_thrr=1  or (ind_thrr=2  and numyr_thr=2)  then i3_thrr_&yr  = i_thrr_&yr;
              if ind_tkrr=1  or (ind_tkrr=2  and numyr_tkr=2)  then i3_tkrr_&yr  = i_tkrr_&yr;
              if ind_xlir=1  or (ind_xlir=2  and numyr_xli=2)  then i3_xlir_&yr  = i_xlir_&yr;
              if ind_ocr=1   or (ind_ocr=2   and numyr_oc=2)   then i3_ocr_&yr   = i_ocr_&yr;
              if ind_clcr=1  or (ind_clcr=2  and numyr_clc=2)  then i3_clcr_&yr  = i_clcr_&yr;
              if ind_ahr=1   or (ind_ahr=2   and numyr_ah=2)   then i3_ahr_&yr   = i_ahr_&yr;
              if ind_vhr=1   or (ind_vhr=2   and numyr_vh=2)   then i3_vhr_&yr   = i_vhr_&yr;
              if ind_pr=1    or (ind_pr=2    and numyr_p=2)    then i3_pr_&yr    = i_pr_&yr;
              if ind_mastr=1 or (ind_mastr=2 and numyr_mast=2) then i3_mastr_&yr = i_mastr_&yr;
              if ind_lumpr=1 or (ind_lumpr=2 and numyr_lump=2) then i3_lumpr_&yr = i_lumpr_&yr;
              if ind_bsr=1   or (ind_bsr=2   and numyr_bs=2)   then i3_bsr_&yr   = i_bsr_&yr;
              if ind_bwlsr=1 or (ind_bwlsr=2 and numyr_bwls=2) then i3_bwlsr_&yr = i_bwlsr_&yr;

            data &outd; set a;
               
                 keep org_id 
                      i3_bcsr_&yr  i3_cmcr_&yr  i3_cdc1r_&yr i3_cdc3r_&yr i3_cdc4r_&yr i3_gsor_&yr
                      i3_opvr_&yr  i3_ervr_&yr  i3_aspr_&yr  i3_orsr_&yr  i3_tdayr_&yr i3_mdayr_&yr i3_sdayr_&yr i3_tdisr_&yr i3_mdisr_&yr i3_sdisr_&yr
                      i3_cabgr_&yr i3_ptcar_&yr i3_ccr_&yr   i3_cer_&yr   i3_rffr_&yr  i3_thrr_&yr  i3_tkrr_&yr  i3_xlir_&yr  i3_ocr_&yr   i3_clcr_&yr
                      i3_ahr_&yr   i3_vhr_&yr   i3_pr_&yr    i3_mastr_&yr i3_lumpr_&yr i3_bsr_&yr   i3_bwlsr_&yr ;

                 proc sort; by org_id;

        %mend;

        %seti3(03,n03); 
        %seti3(04,n04); 
        %seti3(05,n05);
        %seti3(06,n06); 
        %seti3(07,n07); 
        %seti3(08,n08);
        %seti3(09,n09);
        %seti3(10,n10);
        %seti3(11,n11);
        %seti3(12,n12);
 
      data sub03; merge n03 n04 n05 n06 n07 n08 n09 n10 n11 n12; by org_id;
 

      data comb3; merge comb2 sub03; by org_id;

       ind3_bcsr =sum(i3_bcsr_03,i3_bcsr_04,i3_bcsr_05,i3_bcsr_06,i3_bcsr_07,i3_bcsr_08,i3_bcsr_09,i3_bcsr_10,i3_bcsr_11,i3_bcsr_12);
       ind3_cmcr =sum(i3_cmcr_03,i3_cmcr_04,i3_cmcr_05,i3_cmcr_06,i3_cmcr_07,i3_cmcr_08,i3_cmcr_09,i3_cmcr_10,i3_cmcr_11,i3_cmcr_12);
       ind3_cdc1r=sum(i3_cdc1r_03,i3_cdc1r_04,i3_cdc1r_05,i3_cdc1r_06,i3_cdc1r_07,i3_cdc1r_08,i3_cdc1r_09,i3_cdc1r_10,i3_cdc1r_11,i3_cdc1r_12);
       ind3_cdc3r=sum(i3_cdc3r_03,i3_cdc3r_04,i3_cdc3r_05,i3_cdc3r_06,i3_cdc3r_07,i3_cdc3r_08,i3_cdc3r_09,i3_cdc3r_10,i3_cdc3r_11,i3_cdc3r_12);
       ind3_cdc4r=sum(i3_cdc4r_03,i3_cdc4r_04,i3_cdc4r_05,i3_cdc4r_06,i3_cdc4r_07,i3_cdc4r_08,i3_cdc4r_09,i3_cdc4r_10,i3_cdc4r_11,i3_cdc4r_12);
       ind3_gsor =sum(i3_gsor_03,i3_gsor_04,i3_gsor_05,i3_gsor_06,i3_gsor_07,i3_gsor_08,i3_gsor_09,i3_gsor_10,i3_gsor_11,i3_gsor_12);

       ind3_opvr=sum(i3_opvr_03,i3_opvr_04,i3_opvr_05,i3_opvr_06,i3_opvr_07,i3_opvr_08,i3_opvr_09,i3_opvr_10,i3_opvr_11,i3_opvr_12);
       ind3_ervr=sum(i3_ervr_03,i3_ervr_04,i3_ervr_05,i3_ervr_06,i3_ervr_07,i3_ervr_08,i3_ervr_09,i3_ervr_10,i3_ervr_11,i3_ervr_12);
       ind3_aspr=sum(i3_aspr_03,i3_aspr_04,i3_aspr_05,i3_aspr_06,i3_aspr_07,i3_aspr_08,i3_aspr_09,i3_aspr_10,i3_aspr_11,i3_aspr_12);
       ind3_orsr=sum(i3_orsr_03,i3_orsr_04,i3_orsr_05,i3_orsr_06,i3_orsr_07,i3_orsr_08,i3_orsr_09,i3_orsr_10,i3_orsr_11,i3_orsr_12);

       ind3_tdayr=sum(i3_tdayr_03,i3_tdayr_04,i3_tdayr_05,i3_tdayr_06,i3_tdayr_07,i3_tdayr_08,i3_tdayr_09,i3_tdayr_10,i3_tdayr_11,i3_tdayr_12);
       ind3_mdayr=sum(i3_mdayr_03,i3_mdayr_04,i3_mdayr_05,i3_mdayr_06,i3_mdayr_07,i3_mdayr_08,i3_mdayr_09,i3_mdayr_10,i3_mdayr_11,i3_mdayr_12);
       ind3_sdayr=sum(i3_sdayr_03,i3_sdayr_04,i3_sdayr_05,i3_sdayr_06,i3_sdayr_07,i3_sdayr_08,i3_sdayr_09,i3_sdayr_10,i3_sdayr_11,i3_sdayr_12);
       ind3_tdisr=sum(i3_tdisr_03,i3_tdisr_04,i3_tdisr_05,i3_tdisr_06,i3_tdisr_07,i3_tdisr_08,i3_tdisr_09,i3_tdisr_10,i3_tdisr_11,i3_tdisr_12);
       ind3_mdisr=sum(i3_mdisr_03,i3_mdisr_04,i3_mdisr_05,i3_mdisr_06,i3_mdisr_07,i3_mdisr_08,i3_mdisr_09,i3_mdisr_10,i3_mdisr_11,i3_mdisr_12);
       ind3_sdisr=sum(i3_sdisr_03,i3_sdisr_04,i3_sdisr_05,i3_sdisr_06,i3_sdisr_07,i3_sdisr_08,i3_sdisr_09,i3_sdisr_10,i3_sdisr_11,i3_sdisr_12);

       ind3_cabgr=sum(i3_cabgr_03,i3_cabgr_04,i3_cabgr_05,i3_cabgr_06,i3_cabgr_07,i3_cabgr_08,i3_cabgr_09,i3_cabgr_10,i3_cabgr_11,i3_cabgr_12);
       ind3_ptcar=sum(i3_ptcar_03,i3_ptcar_04,i3_ptcar_05,i3_ptcar_06,i3_ptcar_07,i3_ptcar_08,i3_ptcar_09,i3_ptcar_10,i3_ptcar_11,i3_ptcar_12);
       ind3_ccr  =sum(i3_ccr_03,i3_ccr_04,i3_ccr_05,i3_ccr_06,i3_ccr_07,i3_ccr_08,i3_ccr_09,i3_ccr_10,i3_ccr_11,i3_ccr_12);
       ind3_cer  =sum(i3_cer_03,i3_cer_04,i3_cer_05,i3_cer_06,i3_cer_07,i3_cer_08,i3_cer_09,i3_cer_10,i3_cer_11,i3_cer_12);
       ind3_rffr =sum(i3_rffr_03,i3_rffr_04,i3_rffr_05,i3_rffr_06,i3_rffr_07,i3_rffr_08,i3_rffr_09,i3_rffr_10,i3_rffr_11,i3_rffr_12);
       ind3_thrr =sum(i3_thrr_03,i3_thrr_04,i3_thrr_05,i3_thrr_06,i3_thrr_07,i3_thrr_08,i3_thrr_09,i3_thrr_10,i3_thrr_11,i3_thrr_12);
       ind3_tkrr =sum(i3_tkrr_03,i3_tkrr_04,i3_tkrr_05,i3_tkrr_06,i3_tkrr_07,i3_tkrr_08,i3_tkrr_09,i3_tkrr_10,i3_tkrr_11,i3_tkrr_12);
       ind3_xlir =sum(i3_xlir_03,i3_xlir_04,i3_xlir_05,i3_xlir_06,i3_xlir_07,i3_xlir_08,i3_xlir_09,i3_xlir_10,i3_xlir_11,i3_xlir_12);
       ind3_ocr  =sum(i3_ocr_03,i3_ocr_04,i3_ocr_05,i3_ocr_06,i3_ocr_07,i3_ocr_08,i3_ocr_09,i3_ocr_10,i3_ocr_11,i3_ocr_12);
       ind3_clcr =sum(i3_clcr_03,i3_clcr_04,i3_clcr_05,i3_clcr_06,i3_clcr_07,i3_clcr_08,i3_clcr_09,i3_clcr_10,i3_clcr_11,i3_clcr_12);
       ind3_ahr  =sum(i3_ahr_03,i3_ahr_04,i3_ahr_05,i3_ahr_06,i3_ahr_07,i3_ahr_08,i3_ahr_09,i3_ahr_10,i3_ahr_11,i3_ahr_12);
       ind3_vhr  =sum(i3_vhr_03,i3_vhr_04,i3_vhr_05,i3_vhr_06,i3_vhr_07,i3_vhr_08,i3_vhr_09,i3_vhr_10,i3_vhr_11,i3_vhr_12);
       ind3_pr   =sum(i3_pr_03,i3_pr_04,i3_pr_05,i3_pr_06,i3_pr_07,i3_pr_08,i3_pr_09,i3_pr_10,i3_pr_11,i3_pr_12);

       ind3_mastr=sum(i3_mastr_03,i3_mastr_04,i3_mastr_05,i3_mastr_06,i3_mastr_07,i3_mastr_08,i3_mastr_09,i3_mastr_10,i3_mastr_11,i3_mastr_12);
       ind3_lumpr=sum(i3_lumpr_03,i3_lumpr_04,i3_lumpr_05,i3_lumpr_06,i3_lumpr_07,i3_lumpr_08,i3_lumpr_09,i3_lumpr_10,i3_lumpr_11,i3_lumpr_12);
       ind3_bsr  =sum(i3_bsr_03,i3_bsr_04,i3_bsr_05,i3_bsr_06,i3_bsr_07,i3_bsr_08,i3_bsr_09,i3_bsr_10,i3_bsr_11,i3_bsr_12);
       ind3_bwlsr=sum(i3_bwlsr_03,i3_bwlsr_04,i3_bwlsr_05,i3_bwlsr_06,i3_bwlsr_07,i3_bwlsr_08,i3_bwlsr_09,i3_bwlsr_10,i3_bwlsr_11,i3_bwlsr_12);

       proc sort; by org_id;

  data outc; set comb3;

       %macro tab(outdat,yr);

            data t00; set outc;
             
                 if d1bcs_&yr=1  or d2bcs_&yr=1  or i3_bcsr_&yr =1 then exc_bcs_&yr=1; 
                 if d1cmc_&yr=1  or d2cmc_&yr=1  or i3_cmcr_&yr =1 then exc_cmc_&yr=1; 
                 if d1cdc1_&yr=1 or d2cdc1_&yr=1 or i3_cdc1r_&yr=1 then exc_cdc1_&yr=1; 
                 if d1cdc3_&yr=1 or d2cdc3_&yr=1 or i3_cdc3r_&yr=1 then exc_cdc3_&yr=1; 
                 if d1cdc4_&yr=1 or d2cdc4_&yr=1 or i3_cdc4r_&yr=1 then exc_cdc4_&yr=1; 
                 if d1gso_&yr=1  or d2gso_&yr=1  or i3_gsor_&yr =1 then exc_gso_&yr=1; 

                 if d1opv_&yr=1 or d2opv_&yr=1 or i3_opvr_&yr=1 then exc_opv_&yr=1;
                 if d1erv_&yr=1 or d2erv_&yr=1 or i3_ervr_&yr=1 then exc_erv_&yr=1;
                 if d1asp_&yr=1 or d2asp_&yr=1 or i3_aspr_&yr=1 then exc_asp_&yr=1;
                 if d1ors_&yr=1 or d2ors_&yr=1 or i3_orsr_&yr=1 then exc_ors_&yr=1;

                 if d1tday_&yr=1 or d2tday_&yr=1 or i3_tdayr_&yr=1 then exc_tday_&yr=1;
                 if d1mday_&yr=1 or d2mday_&yr=1 or i3_mdayr_&yr=1 then exc_mday_&yr=1;
                 if d1sday_&yr=1 or d2sday_&yr=1 or i3_sdayr_&yr=1 then exc_sday_&yr=1;
                 if d1tdis_&yr=1 or d2tdis_&yr=1 or i3_tdisr_&yr=1 then exc_tdis_&yr=1;
                 if d1mdis_&yr=1 or d2mdis_&yr=1 or i3_mdisr_&yr=1 then exc_mdis_&yr=1;
                 if d1sdis_&yr=1 or d2sdis_&yr=1 or i3_sdisr_&yr=1 then exc_sdis_&yr=1;

                 if d1cabg_&yr=1 or d2cabg_&yr=1 or i3_cabgr_&yr=1 then exc_cabg_&yr=1;
                 if d1ptca_&yr=1 or d2ptca_&yr=1 or i3_ptcar_&yr=1 then exc_ptca_&yr=1;

                 if d1cc_&yr=1   or d2cc_&yr=1   or i3_ccr_&yr=1   then exc_cc_&yr=1;
                 if d1ce_&yr=1   or d2ce_&yr=1   or i3_cer_&yr=1   then exc_ce_&yr=1;
                 if d1rff_&yr=1  or d2rff_&yr=1  or i3_rffr_&yr=1  then exc_rff_&yr=1;
                 if d1thr_&yr=1  or d2thr_&yr=1  or i3_thrr_&yr=1  then exc_thr_&yr=1;
                 if d1tkr_&yr=1  or d2tkr_&yr=1  or i3_tkrr_&yr=1  then exc_tkr_&yr=1;
                 if d1xli_&yr=1  or d2xli_&yr=1  or i3_xlir_&yr=1  then exc_xli_&yr=1;
                 if d1oc_&yr=1   or d2oc_&yr=1   or i3_ocr_&yr=1   then exc_oc_&yr=1;
                 if d1clc_&yr=1  or d2clc_&yr=1  or i3_clcr_&yr=1  then exc_clc_&yr=1;
                 if d1ah_&yr=1   or d2ah_&yr=1   or i3_ahr_&yr=1   then exc_ah_&yr=1;
                 if d1vh_&yr=1   or d2vh_&yr=1   or i3_vhr_&yr=1   then exc_vh_&yr=1;
                 if d1p_&yr=1    or d2p_&yr=1    or i3_pr_&yr=1    then exc_p_&yr=1;
                 if d1mast_&yr=1 or d2mast_&yr=1 or i3_mastr_&yr=1 then exc_mast_&yr=1;
                 if d1lump_&yr=1 or d2lump_&yr=1 or i3_lumpr_&yr=1 then exc_lump_&yr=1;
                 if d1bs_&yr=1   or d2bs_&yr=1   or i3_bsr_&yr=1   then exc_bs_&yr=1;
                 if d1bwls_&yr=1 or d2bwls_&yr=1 or i3_bwlsr_&yr=1 then exc_bwls_&yr=1;

     data &outdat; set t00;

       keep org_id org_name totn_&yr 
            bcs_&yr   i3_bcsr_&yr  exc_bcs_&yr
            cmc_&yr   i3_cmcr_&yr  exc_cmc_&yr
            cdc1_&yr  i3_cdc1r_&yr exc_cdc1_&yr
            cdc3_&yr  i3_cdc3r_&yr exc_cdc3_&yr
            cdc4_&yr  i3_cdc4r_&yr exc_cdc4_&yr
            gso_&yr   i3_gsor_&yr  exc_gso_&yr
            opv_&yr   i3_opvr_&yr  exc_opv_&yr
            erv_&yr   i3_ervr_&yr  exc_erv_&yr
            asp_&yr   i3_aspr_&yr  exc_asp_&yr
            ors_&yr   i3_orsr_&yr  exc_ors_&yr
            tday_&yr  i3_tdayr_&yr exc_tday_&yr
            mday_&yr  i3_mdayr_&yr exc_mday_&yr
            sday_&yr  i3_sdayr_&yr exc_sday_&yr
            tdis_&yr  i3_tdisr_&yr exc_tdis_&yr
            mdis_&yr  i3_mdisr_&yr exc_mdis_&yr
            sdis_&yr  i3_sdisr_&yr exc_sdis_&yr
            cabg_&yr  i3_cabgr_&yr exc_cabg_&yr
            ptca_&yr  i3_ptcar_&yr exc_ptca_&yr
            cc_&yr    i3_ccr_&yr   exc_cc_&yr
            ce_&yr    i3_cer_&yr   exc_ce_&yr
            rff_&yr   i3_rffr_&yr  exc_rff_&yr
            thr_&yr   i3_thrr_&yr  exc_thr_&yr
            tkr_&yr   i3_tkrr_&yr  exc_tkr_&yr
            xli_&yr   i3_xlir_&yr  exc_xli_&yr
            oc_&yr    i3_ocr_&yr   exc_oc_&yr
            clc_&yr   i3_clcr_&yr  exc_clc_&yr
            ah_&yr    i3_ahr_&yr   exc_ah_&yr
            vh_&yr    i3_vhr_&yr   exc_vh_&yr
            p_&yr     i3_pr_&yr    exc_p_&yr
            mast_&yr  i3_mastr_&yr exc_mast_&yr
            lump_&yr  i3_lumpr_&yr exc_lump_&yr
            bs_&yr    i3_bsr_&yr   exc_bs_&yr 
            bwls_&yr  i3_bwlsr_&yr exc_bwls_&yr
            d1bcs_&yr  d2bcs_&yr    
            d1cmc_&yr  d2cmc_&yr     
            d1cdc1_&yr d2cdc1_&yr
            d1cdc3_&yr d2cdc3_&yr   
            d1cdc4_&yr d2cdc4_&yr    
            d1gso_&yr  d2gso_&yr
            d1opv_&yr  d2opv_&yr    
            d1erv_&yr  d2erv_&yr    
            d1asp_&yr  d2asp_&yr   
            d1ors_&yr  d2ors_&yr
            d1tday_&yr d2tday_&yr   
            d1mday_&yr d2mday_&yr    
            d1sday_&yr d2sday_&yr
            d1tdis_&yr d2tdis_&yr   
            d1mdis_&yr d2mdis_&yr    
            d1sdis_&yr d2sdis_&yr
            d1cabg_&yr d2cabg_&yr   
            d1ptca_&yr d2ptca_&yr    
            d1cc_&yr   d2cc_&yr
            d1ce_&yr   d2ce_&yr    
            d1rff_&yr  d2rff_&yr    
            d1thr_&yr  d2thr_&yr
            d1tkr_&yr  d2tkr_&yr   
            d1xli_&yr  d2xli_&yr     
            d1oc_&yr   d2oc_&yr
            d1clc_&yr  d2clc_&yr    
            d1ah_&yr   d2ah_&yr      
            d1vh_&yr   d2vh_&yr
            d1p_&yr    d2p_&yr      
            d1mast_&yr d2mast_&yr    
            d1lump_&yr d2lump_&yr
            d1bs_&yr   d2bs_&yr 
            d1bwls_&yr d2bwls_&yr;

         proc sort; by org_id;

    %mend;

    %tab(t03,03);
    %tab(t04,04);
    %tab(t05,05);
    %tab(t06,06);
    %tab(t07,07);
    %tab(t08,08);
    %tab(t09,09);
    %tab(t10,10);
    %tab(t11,11);
    %tab(t12,12);

  data t00; merge t03 t04 t05 t06 t07 t08 t09 t10 t11 t12; by org_id;

       proc sort; by org_id;

  ** Combine With Other Info **;  

    data a03;  set plan.cntr2003; if plantypec='HMO' and tot_n>=500;  data a3;  set a03;  in_2003=1; keep org_id in_2003; proc sort; by org_id; 
    data a04;  set plan.cntr2004; if plantypec='HMO' and tot_n>=500;  data a4;  set a04;  in_2004=1; keep org_id in_2004; proc sort; by org_id;
    data a05;  set plan.cntr2005; if plantypec='HMO' and tot_n>=500;  data a5;  set a05;  in_2005=1; keep org_id in_2005; proc sort; by org_id;
    data a06;  set plan.cntr2006; if plantypec='HMO' and tot_n>=500;  data a6;  set a06;  in_2006=1; keep org_id in_2006; proc sort; by org_id;
    data a07;  set plan.cntr2007; if plantypec='HMO' and tot_n>=500;  data a7;  set a07;  in_2007=1; keep org_id in_2007; proc sort; by org_id;
    data a08;  set plan.cntr2008; if plantypec='HMO' and tot_n>=500;  data a8;  set a08;  in_2008=1; keep org_id in_2008; proc sort; by org_id;
    data a09;  set plan.cntr2009; if plantypec='HMO' and tot_n>=500;  data a9;  set a09;  in_2009=1; keep org_id in_2009; proc sort; by org_id;
    data a010; set plan.cntr2010; if plantypec='HMO' and tot_n>=500;  data a10; set a010; in_2010=1; keep org_id in_2010; proc sort; by org_id;
    data a011; set plan.cntr2011; if plantypec='HMO' and tot_n>=500;  data a11; set a011; in_2011=1; keep org_id in_2011; proc sort; by org_id;
    data a012; set plan.cntr2012; if plantypec='HMO' and tot_n>=500;  data a12; set a012; in_2012=1; keep org_id in_2012; proc sort; by org_id; 

 data a00; merge a3 a4 a5 a6 a7 a8 a9 a10 a11 a12; by org_id;

      proc sort; by org_id;     

  data new.selectplans; merge t00 a00; by org_id;

       proc freq;
            tables in_2003 in_2004 in_2005 in_2006 in_2007 in_2008 in_2009 in_2010 in_2011 in_2012;

       proc means n nmiss mean min max maxdec=3;
            var bcs_03--d2bwls_12;

endsas;
